使用APScheduler3.0.1 实现定时任务的方法


Posted in Python onJuly 22, 2019

需求是在某一指定的时刻执行操作

网上的建议多为通过调用Scheduler的add_date_job实现

不过APScheduler 3.0.1与之前差异较大, 无法通过上述方法实现

参考 https://apscheduler.readthedocs.org/en/latest/userguide.html APScheduler 3.0.1的userguide 解决问题

from datetime import datetime
import time
import os
 
from apscheduler.schedulers.background import BackgroundScheduler
 
 
def tick():
 print('Tick! The time is: %s' % datetime.now())
 
 
if __name__ == '__main__':
 scheduler = BackgroundScheduler()
 scheduler.add_job(tick, 'interval', seconds=3)
 scheduler.start()
 print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))
 
 try:
  # This is here to simulate application activity (which keeps the main thread alive).
  while True:
   time.sleep(2)
 except (KeyboardInterrupt, SystemExit):
  scheduler.shutdown() # Not strictly necessary if daemonic mode is enabled but should be done if possible

实例的代码实现每3秒执行一次tick方法,虽然与需求不符,但发现add_interval_job在APScheduler 3.0.1中 已经被

scheduler.add_job(tick, 'interval', seconds=3)

取代。

help(scheduler.add_job)得到

add_job(func, trigger=None, args=None, kwargs=None, id=None, name=None, misfire_grace_time=undefined, coalesce=undefined, max_instances=undefined, next_run_time=undefined, jobstore='default', executor='default', replace_existing=False, **trigger_args)
Adds the given job to the job list and wakes up the scheduler if it's already running.
 
Any option that defaults to undefined will be replaced with the corresponding default value when the job is scheduled (which happens when the scheduler is started, or immediately if the scheduler is already running).
 
The func argument can be given either as a callable object or a textual reference in the package.module:some.object format, where the first half (separated by :) is an importable module and the second half is a reference to the callable object, relative to the module.
 
The trigger argument can either be:
the alias name of the trigger (e.g. date, interval or cron), in which case any extra keyword arguments to this method are passed on to the trigger's constructor
an instance of a trigger class

由此可知,第参数为trigger,可取值为 date、interval、cron, **trigger_args为该trigger的构造函数。

通过源码找到DateTrigger 的构造函数

def __init__(self, run_date=None, timezone=None)

所以,只需将指定的时间传入add_job

scheduler.add_job(tick, 'date', run_date='2014-11-11 14:48:00')

以上这篇使用APScheduler3.0.1 实现定时任务的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
基于Django contrib Comments 评论模块(详解)
Dec 08 Python
Python自动化运维之IP地址处理模块详解
Dec 10 Python
tensorflow实现softma识别MNIST
Mar 12 Python
Python对象中__del__方法起作用的条件详解
Nov 01 Python
Python rstrip()方法实例详解
Nov 11 Python
pycharm+PyQt5+python最新开发环境配置(踩坑)
Feb 11 Python
Python按钮的响应事件详解
Mar 04 Python
Python实现简单层次聚类算法以及可视化
Mar 18 Python
树莓派用python中的OpenCV输出USB摄像头画面
Jun 22 Python
Django之路由层的实现
Sep 09 Python
python3 配置logging日志类的操作
Apr 08 Python
Python爬虫实例——爬取美团美食数据
Jul 15 Python
Python定时任务APScheduler的实例实例详解
Jul 22 #Python
基于多进程中APScheduler重复运行的解决方法
Jul 22 #Python
django云端留言板实例详解
Jul 22 #Python
python实现图片中文字分割效果
Jul 22 #Python
django用户登录验证的完整示例代码
Jul 21 #Python
Python Threading 线程/互斥锁/死锁/GIL锁
Jul 21 #Python
详解Django模版中加载静态文件配置方法
Jul 21 #Python
You might like
PHP 出现乱码和Sessions验证问题的解决方法!
2008/12/06 PHP
PHPMailer 中文使用说明小结
2010/01/22 PHP
深入php list()函数的详解
2013/06/05 PHP
解析php入库和出库
2013/06/25 PHP
Thinkphp多文件上传实现方法
2014/10/31 PHP
PHP实现统计在线人数功能示例
2016/10/15 PHP
非常不错的功能强大代码简单的管理菜单美化版
2008/07/09 Javascript
JavaScript 用Node.js写Shell脚本[译]
2012/09/20 Javascript
JSON+HTML实现国家省市联动选择效果
2014/05/18 Javascript
《JavaScript DOM 编程艺术》读书笔记之DOM基础
2015/01/09 Javascript
jQuery使用append在html元素后同时添加多项内容的方法
2015/03/26 Javascript
js判断空对象的实例(超简单)
2016/07/26 Javascript
js中获取jsp表单中radio类型的值简单实例
2016/08/15 Javascript
vue渲染时闪烁{{}}的问题及解决方法
2018/03/28 Javascript
Vue拖拽组件列表实现动态页面配置功能
2019/06/17 Javascript
mpvue实现小程序签到金币掉落动画(api实现)
2019/10/17 Javascript
vue 弹出遮罩层样式实例
2020/07/22 Javascript
[02:41]2015国际邀请赛中国区预选赛观战指南
2015/05/20 DOTA
[47:20]DAC2018 4.4 淘汰赛 Optic vs Mineski 第一场
2018/04/05 DOTA
详解Python import方法引入模块的实例
2017/08/02 Python
对python中的xlsxwriter库简单分析
2018/05/04 Python
python3实现弹弹球小游戏
2019/11/25 Python
在Pytorch中计算自己模型的FLOPs方式
2019/12/30 Python
解决django接口无法通过ip进行访问的问题
2020/03/27 Python
Python 实现PS滤镜的旋涡特效
2020/12/03 Python
H5 canvas实现贪吃蛇小游戏
2017/07/28 HTML / CSS
ECCO英国官网:丹麦鞋履品牌
2019/09/03 全球购物
经典c++面试题三
2015/07/08 面试题
电子商务专业学生的自我鉴定
2013/11/28 职场文书
《美丽的田园》教学反思
2014/03/01 职场文书
八荣八耻演讲稿
2014/09/15 职场文书
小学生安全教育广播稿
2014/10/20 职场文书
三八节活动简报
2015/07/20 职场文书
防震减灾主题班会
2015/08/14 职场文书
导游词之海南-南湾猴岛
2019/10/12 职场文书
python tqdm用法及实例详解
2021/06/16 Python