python 4种定时任务(循环+sleep、Timer、sched、APScheduler) | 您所在的位置:网站首页 › 儿童衣服b类经过清洗可以去除有害物质吗 › python 4种定时任务(循环+sleep、Timer、sched、APScheduler) |
python 4种定时任务(循环+sleep、Timer、sched、APScheduler)
1、循环 + sleep(s):线程睡眠函数,隔 s 秒运行一次程序,陷入死循环 import datetime as dt def task(s): while True: print(dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S")) time.sleep(s) task(2)2、Timer(s, func, ()).start() 第一个参数: 定点:延迟多长时间执行函数(单位:秒) 第二个参数: 函数名称 第三个参数: 函数参数(元组形式) from threading import Timer def func(): # 需要定时执行的函数 print([dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S")]) def task(s): print(dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S")) Timer(s, func, ()).start() task(2)3、sched 事件调度器: (1)构造一个sched.scheduler类 scheduler = sched.scheduler(timefunc, delayfunc) timefunc:当前时间(默认 time.time) delayfunc:暂停运行的时间单元(默认 time.sleep) (2)添加调度任务 ① 定点:延长指定的时间执行任务 scheduler.enter(delay, priority, action, argument=(), kwargs={}) delay:延迟多长时间执行任务(单位:秒) priority:优先级:越小优先级越大 action:函数名称 argument 和 kwargs :函数位置和关键字参数 ② 定时:指定 time 时刻执行任务 scheduler.enterabs(time, priority, action, argument=(), kwargs={}): time:指定时间点 其余参数同上 (3)运行任务 scheduler.run() 具体代码如下: import sched def task(s): print(dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S")) # 初始化 sched 模块的 scheduler 类 scheduler1 = sched.scheduler(time.time, time.sleep) # 增加调度任务 scheduler1.enter(s, 1, func,()) # 运行任务 scheduler1.run() task(2)4、APScheduler: APScheduler 四个组件分别为:调度器(scheduler)、触发器(trigger),作业存储(job store),执行器(executor) (1)新建调度器schedulers BlockingScheduler : 调度器在当前进程的主线程中运行,也就是会阻塞当前线程 BackgroundScheduler : 调度器在后台线程中运行,不会阻塞当前线程 import datetime as dt from apscheduler.schedulers.blocking import BlockingScheduler scheduler = BlockingScheduler()(2)添加调度任务trigger ① date 触发器:(指定时间点触发),参数如下: run_date(datetime或str):任务运行的日期或时间 timezone(datetime.tzinfo或str):指定时区 # 例1:在 2020-9-24 时刻运行一次 func 方法 scheduler.add_job(func, 'date', run_date = dt.date(2020, 9, 24)) # 例2: 在 2020-9-24 15:10:00 时刻运行一次 func 方法 scheduler.add_job(func, 'date', run_date = dt.datetime(2020, 9, 24, 15, 10, 0)) # 例3: 在 2020-9-24 15:11:00 时刻运行一次 func 方法 scheduler.add_job(func, 'date', run_date = '2020-9-24 15:11:00')② interval 触发器: (固定时间间隔触发),参数如下: weeks(int):间隔几周 days(int):间隔几天 hours(int):间隔几小时 minutes(int):间隔几分钟 seconds(int):间隔几秒钟 start_date(datetime或str):开始时间 end_date(datetime或str):结束时间 timezone(datetime.tzinfo或str):时区 # 例1:每隔两分钟执行一次 func 方法 scheduler.add_job(func, 'interval', minutes = 2) # 例2:在 2020-9-24 15:15:00 ~ 2020-9-24 15:20:00 之间, 每隔两分钟执行一次 func 方法 scheduler.add_job(func, 'interval', minutes = 2, start_date = '2020-9-24 15:15:00' , end_date = '2020-9-24 15:20:00')③ cron 触发器:(在指定时间周期性地触发),参数如下: year(int 或 str):年 month(int 或 str):月 day(int 或 str):日 week(int 或 str):周(1-53) day_of_week(int 或 str):星期几(0-6) hour(int 或 str):时 minute(int 或 str):分 second(int 或 str):秒 start_date(datetime或str):最早开始时间(包含) end_date(datetime或str):最晚结束时间(包含) timezone(datetime.tzinfo或str):指定时区 # 例:在每年 1-3、7-9 月份中的每个星期一、二中的 00:00, 01:00, 02:00 和 03:00 执行 func 任务 scheduler.add_job(func, 'cron', month = '1-3,7-9',day='0, tue', hour='0-3')(3)运行调度任务 scheduler.start()总结: 1:循环 + sleep() :陷入死循环 2:timer 定时:只能执行一次,如果想重复执行同一任务, 需重复添加调度任务 3:schedule 定点、定时:只能执行一次,如果想重复执行同一任务, 需重复添加调度任务 4:APScheduler 定点、定时:(四个组件分别为:触发器(trigger),作业存储器(job store),执行器(executor),调度器(scheduler)) (1)job stores:对调度任务的管理: ① 添加job: # add_job():可以改变或者移除 job scheduler.add_job(func, 'interval', minutes = 2) # scheduled_job():只适用于应用运行期间不会改变的 job scheduler.scheduled_job(func, 'interval', minutes = 2)②移除job: # remove_job() :根据 job 的 id 来移除,所以要在 job 创建的时候指定一个 id scheduler.add_job(func, 'interval', minutes = 2, id = 'job_one') scheduler.remove_job(job_one) # job.remove() :对 job 执行 remove 方法 job = add_job(func, 'interval', minutes = 2, id = 'job_one') job.remvoe()③ 暂停job: apscheduler.job.Job.pause() apscheduler.schedulers.base.BaseScheduler.pause_job()④ 恢复job: apscheduler.job.Job.resume() apscheduler.schedulers.base.BaseScheduler.resume_job()⑤ 修改job: # modify_job() scheduler.modify_job('job_one', minutes = 5) # job.modify() job = scheduler.add_job(func, 'interval', minutes = 2) job.modify(minutes = 5)⑥ 关闭job: scheduler.shutdown() scheduler.shutdown(wait=false)(2)executors:执行调度任务的模块,常用的 executor 有两种: ProcessPoolExecutor ThreadPoolExecutor 想知道更多,请关注我的公众号~ |
CopyRight 2018-2019 实验室设备网 版权所有 |