任务管理工具之APScheduler
最近更新:2025-08-11
|
字数总计:990
|
阅读估时:4分钟
|
阅读量: 次
APScheduler之定时任务工具 APScheduler概述
1.安装
2.使用方式 1 2 3 4 5 6 7 8 9 10 11 12 13 14 from apscheduler.schedulers.background import BackgroundSchedulerscheduler = BackgroundScheduler() def my_job (param1, param2 ): pass scheduler.add_job(my_job, 'date' , args=[100 , 'python' ]) scheduler.start()
3.调度器Scheduler 负责管理定时任务
BlockingScheduler: 作为独立进程时使用
1 2 3 4 from apscheduler.schedulers.blocking import BlockingSchedulerscheduler = BlockingScheduler() scheduler.start()
BackgroundScheduler: 在框架程序(如Django、Flask)中使用
1 2 3 4 from apscheduler.schedulers.background import BackgroundSchedulerscheduler = BackgroundScheduler() scheduler.start()
4.执行器 executors 在定时任务该执行时,以进程或线程方式执行任务
1 2 3 4 from apscheduler.executors.pool import ThreadPoolExecutorThreadPoolExecutor(max_workers) ThreadPoolExecutor(20 )
使用方法
1 2 3 4 executors = { 'default' : ThreadPoolExecutor(20 ) } scheduler = BackgroundScheduler(executors=executors)
1 2 3 4 from apscheduler.executors.pool import ProcessPoolExecutorProcessPoolExecutor(max_workers) ProcessPoolExecutor(5 )
使用方法
1 2 3 4 executors = { 'default' : ProcessPoolExecutor(3 ) } scheduler = BackgroundScheduler(executors=executors)
5.触发器 Trigger 指定定时任务执行的时机
1) date 在特定的时间日期执行
1 2 3 4 5 6 7 8 9 10 11 12 from datetime import datesched.add_job(my_job, 'date' , run_date=date(2009 , 11 , 6 )) sched.add_job(my_job, 'date' , run_date=datetime(2009 , 11 , 6 , 16 , 30 , 5 )) sched.add_job(my_job, 'date' , run_date='2009-11-06 16:30:05' ) sched.add_job(my_job, 'date' ) sched.start()
2) interval 经过指定的时间间隔执行
weeks (int) – number of weeks to wait
days (int) – number of days to wait
hours (int) – number of hours to wait
minutes (int) – number of minutes to wait
seconds (int) – number of seconds to wait
start_date (datetime|str) – starting point for the interval calculation
end_date (datetime|str) – latest possible date/time to trigger on
timezone (datetime.tzinfo|str) – time zone to use for the date/time calculations
1 2 3 4 5 6 7 from datetime import datetimesched.add_job(job_function, 'interval' , hours=2 ) sched.add_job(job_function, 'interval' , hours=2 , start_date='2010-10-10 09:30:00' , end_date='2014-06-15 11:00:00' )
3) cron 按指定的周期执行
year (int|str) – 4-digit year
month (int|str) – month (1-12)
day (int|str) – day of the (1-31)
week (int|str) – ISO week (1-53)
day_of_week (int|str) – number or name of weekday (0-6 or mon,tue,wed,thu,fri,sat,sun)
hour (int|str) – hour (0-23)
minute (int|str) – minute (0-59)
second (int|str) – second (0-59)
start_date (datetime|str) – earliest possible date/time to trigger on (inclusive)
end_date (datetime|str) – latest possible date/time to trigger on (inclusive)
timezone (datetime.tzinfo|str) – time zone to use for the date/time calculations (defaults to scheduler timezone)
1 2 3 4 5 sched.add_job(job_function, 'cron' , month='6-8,11-12' , day='3rd fri' , hour='0-3' ) sched.add_job(job_function, 'cron' , day_of_week='mon-fri' , hour=5 , minute=30 , end_date='2014-05-30' )
6. 配置方法 方法1 1 2 3 4 5 6 7 from apscheduler.schedulers.background import BackgroundSchedulerfrom apscheduler.executors.pool import ThreadPoolExecutorexecutors = { 'default' : ThreadPoolExecutor(20 ), } scheduler = BackgroundScheduler(executors=executors)
方法2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 from pytz import utcfrom apscheduler.schedulers.background import BackgroundSchedulerfrom apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStorefrom apscheduler.executors.pool import ProcessPoolExecutorexecutors = { 'default' : {'type' : 'threadpool' , 'max_workers' : 20 }, 'processpool' : ProcessPoolExecutor(max_workers=5 ) } scheduler = BackgroundScheduler() scheduler.configure(executors=executors)
7. 启动
对于BlockingScheduler ,程序会阻塞在这,防止退出
对于BackgroundScheduler,程序会立即返回,后台运行
8.扩展 任务管理 方式一 1 2 3 4 job = scheduler.add_job(myfunc, 'interval' , minutes=2 ) job.remove() job.pause() job.resume()
方式二 1 2 3 4 scheduler.add_job(myfunc, 'interval' , minutes=2 , id ='my_job_id' ) scheduler.remove_job('my_job_id' ) scheduler.pause_job('my_job_id' ) scheduler.resume_job('my_job_id' )
调整任务调度周期 1 2 3 job.modify(max_instances=6 , name='Alternate name' ) scheduler.reschedule_job('my_job_id' , trigger='cron' , minute='*/5' )
停止APScheduler运行