APscgeduler
| 全名:advanceded python scheduler 是一款python开发的定时器工具 |
| 有python实现的,不是由linux系统维护的,是单独的线程的方式,在进程中进行管理 |
| 优点: |
| 1.不依赖linux系统的crontab系统定时 |
| 2.可以动态的添加定时任务,如 |
| 下单30内必须支付,否则取消订单,恢复库存 |
| 在用户下单时刻起,创建一个30分钟的定时任务,任务功能可以到30种后判断用户的订单状态没如果没有支付,取消订单(动态的定时任务) |
| 3.对添加定时任务可以永久保存 |
| |
| 定时任务有两种 |
| 1.定时任务页面静态化,在django运行起来,我们明确知道有这个定时任务,不是动态添加 |
| 一般可以直接采用linux crontab |
| 使用APSchudler |
| 2.动态定时任务 |
| 使用crontab不是很方便,使用APSchudler |
基本安装使用
4个概念
触发器
| 1.使用date在特定时间进行执行 可以规定时间是年月日时分秒进行执行 |
| |
| 2.interval 经过特定时间的周期 规定每隔几天 几分钟 几个月进行执行 |
| |
| 3.cron制定的周期执行 可以规定在每周的几点几分进行执行一次 |
3种方式
| from apscheduler.schedulers.blocking import BlockingScheduler |
| |
| sched = BlockingScheduler() |
| |
| |
| def show(): |
| print(123) |
| |
| |
| |
| ''' |
| 只要规定好时间,到后就会直接调用当前的函数对象obj,执行函数 |
| ''' |
| |
| |
| |
| |
| |
| |
| |
| |
| 例如:sched.add_job(show,'interval',hour='3') |
| 没隔3个小时执行一次,每隔3个小时执行一次 |
| ''' |
| # 使用的int类型进行使用的 |
| weeks(int) 等待的星期天数 |
| days(int ) 等待的天数 |
| hours(int) 等待的小时 |
| minutes(int) 等待分钟 |
| seconds(int) 等待秒 |
| # 使用字符串类型使用的 |
| start_date(datetime|str) 区间计算的起点 |
| end_date(datetime|str) 可能启动的最新日期/时间 |
| timezone(datetime.tzinfo|str) 用于日期/时间计算的时区 |
| jitter(int|None) 作业执行时间最多提前或延迟抖动秒 |
| ''' |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| 例如:sched.add_job(show,'cron',hour='3') |
| 因为没有设置月份年份,周等属性,那么就会每天进行到3点执行一次 |
| ''' |
| year(int|str) 按年 |
| month(int|str) 按照月1-12 |
| day(int|str) 按照天1-31 |
| week(int|str) 按照周1-53 |
| day_of_week(int|str) 按照星期天(0-6) fri |
| hour(int|str) 按照小时0-23 |
| minute(int|str) 按照分钟0-59 |
| second(int|str) 按照分钟 |
| |
| start_date(datetime|str) 区间计算的起点 |
| end_date(datetime|str) 可能启动的最新日期/时间 |
| timezone(datetime.tzinfo|str) 用于日期/时间计算的时区 |
| jitter(int|None) 作业执行时间最多提前或延迟抖动秒 |
| ''' |
| |
| |
| |
任务存储器
| 任务存储的设置 |
| 1. MemoryJobStore 默认 内存 |
| |
| 2.MongoDBJobStore |
| from apscheduler.jobstores.mongodb import MongoDBJobStore |
| MongoDBJobStore() |
| |
| 3.redis |
| from apscheduler.jobstores.redis import RedisJobStore |
| RedisJobStore() |
| |
| 4.关系型数据存储 |
| |
| from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore |
| SQLAlchemyJobStore() |
| SQLAlchemyJobStore(url='对接的数据库地址') |
执行器
| from apscheduler.executors.pool import ThreadPoolExecutor |
| ThreadPoolExecutor 线程方式 |
| ThreadPoolExecutor(max_workers) |
| ThreadPoolExecutor(20) |
| |
| |
| from apscheduler.executors.pool import ProcessPoolExecutor |
| ProcessPoolExecutor 进程方式 |
| ProcessPoolExecutor(max_workers) |
| ProcessPoolExecutor(5) |
| |
调度器
| from apscheduler.schedulers.blocking import BlockingScheduler |
| |
| BlockingScheduler 单独的进程方式使用的(单独的程序使用) |
| |
| from apscheduler.schedulers.background import BackgroundScheduler |
| |
| BackgroundScheduler 不在以单独的进程使用,可以作为引用程序的一部分使用 |
| |
| BackgroundScheduler是动态添加的话是存在返回值的 |
| |
| 案例: |
| |
| from apscheduler.schedulers.background import BackgroundScheduler |
| |
| scheduler = BackgroundScheduler() |
| |
| |
| def show(): |
| print(123) |
| |
| |
| job = b.add_job(show, 'interval', seconds=10) |
| print(job) |
| |
| |
| |
| |
| |
| job = b.add_job(show, 'interval', seconds=10,id='my_wkx') |
| print(job) |
| |
| |
| |
| |
| |
| |
| |
| 返回对象调整 |
| job.modify(max_instances=6,name='Alternate name') |
| 调度器对象根据属性id调整 |
| scheduler.reschedule_job('my_wkx',trigger='cron',minute='*/5') |
| |
| |
| scheduler.start() |
| |
| |
| scheduler.shutdown() |
| |
| |
| scheduler.get_jobs() |
简易版使用(阻塞定时器使用)
| 1.创建调度器对象 |
| 2.创建调度器存储方式 |
| 3.创建调度器使用线程还是进程 |
| 4.是否有默认启动任务 |
| 5.将设置的调度器存储 执行器进行配置 |
| 6.执行 |
| |
| |
| |
| from apscheduler.executors.pool import ProcessPoolExecutor |
| from apscheduler.schedulers.blocking import BlockingScheduler |
| |
| |
| executors = { |
| |
| |
| 'default': ProcessPoolExecutor(3) |
| } |
| |
| ''' |
| Blocking 属于阻塞的调度器 |
| ''' |
| scheduler = BlockingScheduler(executors=executors) |
| |
| |
| def show(): |
| print(123) |
| |
| |
| |
| scheduler.add_job(show, 'interval', seconds=10) |
| |
| if __name__ == '__main__': |
| |
| |
| scheduler.start() |
| scheduler.shutdown() |
| |
| |
配置
| from pytz import utc |
| |
| |
| from apscheduler.executors.pool import ThreadPoolExecutor, ProcessPoolExecutor |
| from apscheduler.jobstores.mongodb import MongoDBJobStore |
| from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore |
| from apscheduler.schedulers.background import BackgroundScheduler |
| |
| |
| |
| |
| |
| |
| jobstores = { |
| |
| 'mongo': MongoDBJobStore(), |
| |
| 'default': SQLAlchemyJobStore(url='关系型数据库的地址') |
| } |
| |
| |
| executors = { |
| 'default': ThreadPoolExecutor(20), |
| 'processpool': ProcessPoolExecutor(5) |
| } |
| |
| |
| job_defaults = { |
| 'coalesce': False, |
| 'max_instances': 3 |
| } |
| |
| scheduler = BackgroundScheduler(jobstores=jobstores, executors=executors, job_defaults=job_defaults) |
| |
| |
| scheduler = BackgroundScheduler() |
| scheduler.configure(jobstores=jobstores, executors=executors, job_defaults=job_defaults) |
| |
| |
| scheduler = BackgroundScheduler({ |
| 'apscheduler.jobstores.mongo': { |
| 'type': 'mongodb' |
| }, |
| 'apscheduler.jobstores.default': { |
| 'type': 'sqlalchemy', |
| 'url': '关系型数据库地址' |
| }, |
| 'apscheduler.executors.default': { |
| 'class': 'apscheduler.executors.pool:ThreadPoolExecutor', |
| 'max_workers': '20' |
| }, |
| 'apscheduler.executors.processpool': { |
| 'type': 'processpool', |
| 'max_workers': '5' |
| }, |
| 'apscheduler.job_defaults.coalesce': 'false', |
| 'apscheduler.job_defaults.max_instances': '3', |
| 'apscheduler.timezone': 'UTC' |
| |
| } |
| ) |
| |
| |
| 配置名为“mongo”的MongoDBJobStore作业存储器 |
| 配置名为“default”的SQLAlchemyJobStore(使用SQLite) |
| 配置名为“default”的ThreadPoolExecutor,最大线程数为20 |
| 配置名为“processpool”的ProcessPoolExecutor,最大进程数为5 |
| UTC作为调度器的时区 from pytz import utc |
| coalesce默认情况下关闭 |
| 作业的默认最大运行实例限制为3 |
动态添加参数
| from apscheduler.schedulers.blocking import BlockingScheduler |
| |
| a = BlockingScheduler () |
| |
| def my_job(a1,a2) |
| pass |
| |
| a.add_job(my_job,'date',args=[100,200]) |
| |
| a.start() |
比较详情:https://blog.csdn.net/kobepaul123/article/details/123616575
比较详情的:https://blog.csdn.net/somezz/article/details/83104368?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-1-83104368-blog-123616575.pc_relevant_without_ctrlist_v3&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-1-83104368-blog-123616575.pc_relevant_without_ctrlist_v3&utm_relevant_index=1
比较详情:https://www.cnblogs.com/KdeS/p/13158961.html#autoid-0-1-0
https://www.cnblogs.com/leffss/p/11912364.html
本文作者:_wangkx
本文链接:https://www.cnblogs.com/kaixinblog/p/16246499.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!