APScheduler的使用
目录
定时框架APScheduler
APScheduler有四种组件
APScheduler提供了七种调度器
APScheduler提供了四种存储方式
APScheduler提供了三种任务触发器
示例示例1 BlockingScheduler
定义job def jod1(): print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) + ' #####job1 ' + str(random.randint(0,10))) 定义run方法 def run1(): scheduler = BlockingScheduler() scheduler.add_job(jod1,'interval',seconds=5) scheduler.start() 完整代码 import time import random from apscheduler.schedulers.blocking import BlockingScheduler def jod1(): print(time.strftime('%Y-%m-%d %H:%M:%S',10))) def run1(): scheduler = BlockingScheduler() scheduler.add_job(jod1,seconds=5) scheduler.start() if __name__ == '__main__': run1() 运行结果 2019-05-16 11:09:05 #####job1 2 2019-05-16 11:09:10 #####job1 5 2019-05-16 11:09:15 #####job1 1 2019-05-16 11:09:20 #####job1 0 2019-05-16 11:09:25 #####job1 8 2019-05-16 11:09:30 #####job1 0 2019-05-16 11:09:35 #####job1 9 2019-05-16 11:09:40 #####job1 1 2019-05-16 11:09:45 #####job1 4 ...... 测试1
job2示例代码 def jod2(): print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) + ' #####job2 ' + str(random.randint(0,10))) run2示例代码 def run2(): scheduler = BlockingScheduler() scheduler.add_job(jod1,seconds=5) scheduler.add_job(jod2,seconds=5) scheduler.start() 实行结果: 2019-05-16 11:10:46 #####job2 9 2019-05-16 11:10:46 #####job1 2 2019-05-16 11:10:51 #####job2 3 2019-05-16 11:10:51 #####job1 2 2019-05-16 11:10:56 #####job2 9 2019-05-16 11:10:56 #####job1 1 2019-05-16 11:11:01 #####job2 9 2019-05-16 11:11:01 #####job1 1 2019-05-16 11:11:06 #####job2 2 2019-05-16 11:11:06 #####job1 5 2019-05-16 11:11:11 #####job2 6 2019-05-16 11:11:11 #####job1 7 2019-05-16 11:11:16 #####job2 1 2019-05-16 11:11:16 #####job1 6 ...... 从执行结果来看,在同一个调度器中添加2个job,这两个job会在同一时刻同时执行 测试2
修改后的job1 def jod1(): print(time.strftime('%Y-%m-%d %H:%M:%S',10))) time.sleep(6) run1 不变,运行结果 2019-05-16 11:18:11 #####job1 1 Execution of job "jod1 (trigger: interval[0:00:05],next run at: 2019-05-16 11:18:16 CST)" skipped: maximum number of running instances reached (1) 2019-05-16 11:18:21 #####job1 9 Execution of job "jod1 (trigger: interval[0:00:05],next run at: 2019-05-16 11:18:26 CST)" skipped: maximum number of running instances reached (1) 2019-05-16 11:18:31 #####job1 5 Execution of job "jod1 (trigger: interval[0:00:05],next run at: 2019-05-16 11:18:36 CST)" skipped: maximum number of running instances reached (1) 2019-05-16 11:18:41 #####job1 0 Execution of job "jod1 (trigger: interval[0:00:05],next run at: 2019-05-16 11:18:46 CST)" skipped: maximum number of running instances reached (1) 从执行结果来看,由于job的运行时间超过了执行器需要执行时的时间,所以本次任务跳过,就变成了10秒执行一次的任务了。 示例2 BackgroundScheduler完整示例: import time import random from apscheduler.schedulers.blocking import BlockingScheduler from apscheduler.schedulers.background import BackgroundScheduler def jod1(): print(time.strftime('%Y-%m-%d %H:%M:%S',10))) time.sleep(6) def run3(): scheduler = BackgroundScheduler() scheduler.add_job(jod1,seconds=5) scheduler.start() while True: print('main-start:',time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))) time.sleep(2) print('main-end:',time.localtime(time.time()))) if __name__ == '__main__': # run1() # run2() run3() 测试1开启一个任务,然后另外开启一个线程 def run3(): scheduler = BackgroundScheduler() scheduler.add_job(jod1,time.localtime(time.time()))) 运行结果 main-start: 2019-05-16 18:42:13 main-end: 2019-05-16 18:42:15 main-start: 2019-05-16 18:42:15 main-end: 2019-05-16 18:42:17 main-start: 2019-05-16 18:42:17 2019-05-16 18:42:18 #####job1 6 main-end: 2019-05-16 18:42:19 main-start: 2019-05-16 18:42:19 main-end: 2019-05-16 18:42:21 main-start: 2019-05-16 18:42:21 Execution of job "jod1 (trigger: interval[0:00:05],next run at: 2019-05-16 18:42:23 CST)" skipped: maximum number of running instances reached (1) main-end: 2019-05-16 18:42:23 main-start: 2019-05-16 18:42:23 main-end: 2019-05-16 18:42:25 main-start: 2019-05-16 18:42:25 main-end: 2019-05-16 18:42:27 main-start: 2019-05-16 18:42:27 2019-05-16 18:42:28 #####job1 5 main-end: 2019-05-16 18:42:29 main-start: 2019-05-16 18:42:29 main-end: 2019-05-16 18:42:31 main-start: 2019-05-16 18:42:31 Execution of job "jod1 (trigger: interval[0:00:05],next run at: 2019-05-16 18:42:33 CST)" skipped: maximum number of running instances reached (1) main-end: 2019-05-16 18:42:33 main-start: 2019-05-16 18:42:33 main-end: 2019-05-16 18:42:35 main-start: 2019-05-16 18:42:35 main-end: 2019-05-16 18:42:37 main-start: 2019-05-16 18:42:37 2019-05-16 18:42:38 #####job1 1 ...... 示例3 采用cron的方式采用cron的方式来调度任务 def run4(): scheduler = BackgroundScheduler() scheduler.add_job(jod1,'cron',day_of_week='fri',second='*/5') scheduler.start() while True: print('main-start:',time.localtime(time.time()))) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- linux – UNIX环境中高级编程的工作代码,最好是在Ubuntu /
- linux – Mysql GTID复制停止工作
- 在sed中使用bash变量作为范围
- linux – configure:错误:找不到php-config.请使用–with
- 嵌入式 – arm-linux-gcc vs arm-elf-gcc
- linux – scsi和/ dev / disk / by-id下相同硬盘的ata条目
- linux系统加固
- linux 安装Python3
- Cannot find autoconf. Please check your autoconf instal
- 自动杀死消耗过多内存或在linux上停顿的进程