Python定时任务APScheduler安装及使用解析
作者:不放弃自己 时间:2023-07-15 02:49:00
1、简介
APScheduler是一个 Python 定时任务框架,使用起来十分方便。提供了基于日期、固定时间间隔以及 crontab 类型的任务,并且可以持久化任务、并以 daemon 方式运行应用。
2、APScheduler四个组件
APScheduler 四个组件分别为:触发器(trigger),作业存储(job store),执行器(executor),调度器(scheduler)。
触发器(trigger)
包含调度逻辑,每一个作业有它自己的触发器,用于决定接下来哪一个作业会运行。除了他们自己初始配置意外,触发器完全是无状态的
APScheduler 有三种内建的 trigger:
date: 特定的时间点触发
interval: 固定时间间隔触发
cron: 在特定时间周期性地触发
作业存储(job store)
存储被调度的作业,默认的作业存储是简单地把作业保存在内存中,其他的作业存储是将作业保存在数据库中。一个作业的数据讲在保存在持久化作业存储时被序列化,并在加载时被反序列化。调度器不能分享同一个作业存储。
APScheduler 默认使用 MemoryJobStore,可以修改使用 DB 存储方案
执行器(executor)
处理作业的运行,他们通常通过在作业中提交制定的可调用对象到一个线程或者进城池来进行。当作业完成时,执行器将会通知调度器。
最常用的 executor 有两种:
ProcessPoolExecutor
ThreadPoolExecutor
调度器(scheduler)
通常在应用中只有一个调度器,应用的开发者通常不会直接处理作业存储、调度器和触发器,相反,调度器提供了处理这些的合适的接口。配置作业存储和执行器可以在调度器中完成,例如添加、修改和移除作业
2、安装
$ pip install apscheduler
接下来我们看下简单的几个示例:
===============interval: 固定时间间隔触发===============
from apscheduler.schedulers.blocking import BlockingScheduler
from datetime import datetime
def job():
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
# 定义BlockingScheduler
sched = BlockingScheduler()
sched.add_job(job, 'interval', seconds=5)
sched.start()
===============cron: 特定时间周期性地触发===============
import time
from apscheduler.schedulers.blocking import BlockingScheduler
def job(text):
t = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
print('{} --- {}'.format(text, t))
scheduler = BlockingScheduler()
# 在每天22点,每隔 1分钟 运行一次 job 方法
scheduler.add_job(job, 'cron', hour=17, minute='*/1', args=['job1'])
# 在每天22和23点的25分,运行一次 job 方法
scheduler.add_job(job, 'cron', hour='22-23', minute='25', args=['job2'])
scheduler.start()
通过装饰器scheduled_job()添加方法
import time
from apscheduler.schedulers.blocking import BlockingScheduler
scheduler = BlockingScheduler()
@scheduler.scheduled_job('interval', seconds=5)
def job1():
t = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
print('job1 --- {}'.format(t))
@scheduler.scheduled_job('cron', second='*/7')
def job2():
t = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
print('job2 --- {}'.format(t))
scheduler.start()
来源:https://www.cnblogs.com/hghua/p/13268007.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
PHP PDOStatement::bindColumn讲解
pytorch模型部署 pth转onnx的方法
Python实现的数据结构与算法之基本搜索详解
![](https://img.aspxhome.com/file/2023/4/67934_0s.png)
Python正则表达式学习小例子
![](https://img.aspxhome.com/file/2023/3/79553_0s.png)
matplotlib 三维图表绘制方法简介
![](https://img.aspxhome.com/file/2023/3/83873_0s.jpg)
实例代码讲解Python 线程池
python opencv图像处理(素描、怀旧、光照、流年、滤镜 原理及实现)
![](https://img.aspxhome.com/file/2023/0/91840_0s.png)
在python中利用try..except来代替if..else的用法
Python利用PyQT5设置闹钟功能
![](https://img.aspxhome.com/file/2023/1/110191_0s.png)
Python+tkinter使用40行代码实现计算器功能
![](https://img.aspxhome.com/file/2023/3/108043_0s.jpg)
Python实现自动化处理Word文档的方法详解
![](https://img.aspxhome.com/file/2023/2/72782_0s.jpg)
Python中使用threading.Event协调线程的运行详解
重构Python代码的六个实例
python的exec、eval使用分析
Pytest mark使用实例及原理解析
python opencv将多个图放在一个窗口的实例详解
![](https://img.aspxhome.com/file/2023/4/105594_0s.jpg)
Python实现基于HTTP文件传输实例
让SQL Server数据库自动执行管理任务(二)
php短视频源码之瀑布流分割线左右间距问题及解决代码
Python实现识别图像中人物的示例代码
![](https://img.aspxhome.com/file/2023/9/63609_0s.jpg)