python定时任务 任务调度 定时模块APScheduler
APScheduler
安装
pip install apscheduler
apscheduler定时机制
- date:在魔偶个特定时间执行一次,支持UNIX系统中的Crontab时间格式
- interval:以固定的时间间隔执行,时间支持秒、分、时、周
- cron:crontab时间格式一样
常用模式
cron
from apscheduler.schedulers.blocking import BlockingScheduler
import datetime
def show_time():
now = datetime.datetime.now()
print(f"hello world{
now.strftime('%Y-%m-%d %H:%M:%S')}")
if __name__ == '__main__':
sched = BlockingScheduler()
sched.add_job(show_time, 'cron', hour='*', minute='*', second='*/2')
sched.start()
# hello world2020-10-15 07:48:12
# hello world2020-10-15 07:48:14
# hello world2020-10-15 07:48:16
# hello world2020-10-15 07:48:18
从运行结果得知,本次任务是时间间隔2秒的任务被成功执行,使用方法采用`add_job`,绑定执行任务,按照crontab时间格式设置定时任务,也支持按周来执行任务。
**传送门**:[APScheduler文档][APScheduler]
还没有评论,来说两句吧...