使用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 相关文章推荐
python在Windows8下获取本机ip地址的方法
Mar 14 Python
python开发之函数定义实例分析
Nov 12 Python
Python使用QRCode模块生成二维码实例详解
Jun 14 Python
详解Python实现多进程异步事件驱动引擎
Aug 25 Python
django 发送手机验证码的示例代码
Apr 25 Python
详解django的serializer序列化model几种方法
Oct 16 Python
python版百度语音识别功能
Jul 09 Python
python实现的爬取电影下载链接功能示例
Aug 26 Python
python使用Geany编辑器配置方法
Feb 21 Python
Python Serial串口基本操作(收发数据)
Nov 06 Python
python编写函数注意事项总结
Mar 29 Python
python数据处理之Pandas类型转换
Apr 28 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的array_diff()函数在处理大数组时的效率问题
2011/11/27 PHP
ASP和PHP实现生成网站快捷方式并下载到桌面的方法
2014/05/08 PHP
php基于Fleaphp框架实现cvs数据导入MySQL的方法
2016/02/23 PHP
php中static和const关键字用法分析
2016/12/07 PHP
让iframe自适应高度(支持XHTML,支持FF)
2007/07/24 Javascript
JS 操作符整理[推荐收藏]
2011/11/15 Javascript
基于jQuery选择器的整理集合
2013/04/26 Javascript
用html5 js实现点击一个按钮达到浏览器全屏效果
2014/05/28 Javascript
Javascript学习笔记之数组的构造函数
2014/11/23 Javascript
使用Chart.js图表库制作漂亮的响应式表单
2015/10/28 Javascript
jQuery实现元素拖拽并cookie保存顺序的方法
2016/02/20 Javascript
JQ选择器_选择同类元素的第N个子元素的实现方法
2016/09/08 Javascript
使用snowfall.jquery.js实现爱心满屏飞的效果
2017/01/05 Javascript
JS组件系列之JS组件封装过程详解
2017/04/28 Javascript
vue2的todolist入门小项目的详细解析
2017/05/11 Javascript
基于JavaScript实现微信抢红包功能
2017/07/20 Javascript
ubuntu编译nodejs所需的软件并安装
2017/09/12 NodeJs
详解Nodejs 通过 fs.createWriteStream 保存文件
2017/10/10 NodeJs
微信小程序录音与播放录音功能
2017/12/25 Javascript
vue.js 中使用(...)运算符报错的解决方法
2018/08/09 Javascript
微信开发之微信jssdk录音功能开发示例
2018/10/22 Javascript
JavaScript进阶(二)词法作用域与作用域链实例分析
2020/05/09 Javascript
js将日期格式转换为YYYY-MM-DD HH:MM:SS
2020/09/18 Javascript
基于openlayers实现角度测量功能
2020/09/28 Javascript
微信小程序基于ColorUI构建皮皮虾短视频去水印组件
2020/11/04 Javascript
Python Web服务器Tornado使用小结
2014/05/06 Python
Python中使用第三方库xlrd来读取Excel示例
2015/04/05 Python
讲解Python中的标识运算符
2015/05/14 Python
python 常见的排序算法实现汇总
2020/08/21 Python
室内设计专业个人的自我评价
2013/12/18 职场文书
初中班主任经验交流材料
2014/05/16 职场文书
低碳环保标语
2014/06/12 职场文书
荆州古城导游词
2015/02/06 职场文书
留学推荐信(中英文版)
2015/03/26 职场文书
爱心募捐通知范文
2015/04/27 职场文书
vue3 自定义图片放大器效果的示例代码
2022/07/23 Vue.js