使用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多进程机制实例详解
Jul 02 Python
搭建Python的Django框架环境并建立和运行第一个App的教程
Jul 02 Python
python fabric实现远程部署
Jan 05 Python
利用Python3分析sitemap.xml并抓取导出全站链接详解
Jul 04 Python
python爬虫之模拟登陆csdn的实例代码
May 18 Python
使用python的pexpect模块,实现远程免密登录的示例
Feb 14 Python
详解Python3网络爬虫(二):利用urllib.urlopen向有道翻译发送数据获得翻译结果
May 07 Python
python腾讯语音合成实现过程解析
Aug 01 Python
FFrpc python客户端lib使用解析
Aug 24 Python
tensorflow指定CPU与GPU运算的方法实现
Apr 21 Python
python 引用传递和值传递详解(实参,形参)
Jun 05 Python
Pytest中skip skipif跳过用例详解
Jun 30 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实现paypal整合方法
2010/11/28 PHP
PHP读取配置文件类实例(可读取ini,yaml,xml等)
2015/07/28 PHP
PHP使用Mysqli类库实现完美分页效果的方法
2016/04/07 PHP
深入浅析yii2-gii自定义模板的方法
2016/04/26 PHP
屏蔽PHP默认设置中的Notice警告的方法
2016/05/20 PHP
php设计模式之策略模式应用案例详解
2019/06/17 PHP
laravel框架数据库配置及操作数据库示例
2019/10/10 PHP
StringTemplate遇见jQuery冲突的解决方法
2011/09/22 Javascript
在javaScript中关于submit和button的区别介绍
2013/10/20 Javascript
JavaScript修改css样式style动态改变元素样式
2013/12/16 Javascript
使用JQUERY进行后台页面布局控制DIV实现左右式
2014/01/07 Javascript
php,js,css字符串截取的办法集锦
2014/09/26 Javascript
JavaScript实现检查页面上的广告是否被AdBlock屏蔽了的方法
2014/11/03 Javascript
利用n 升级工具升级Node.js版本及在mac环境下的坑
2017/02/15 Javascript
bootstrap Validator 模态框、jsp、表单验证 Ajax提交功能
2017/02/17 Javascript
微信小程序page的生命周期和音频播放及监听实例详解
2017/04/07 Javascript
解决vue+ element ui 表单验证有值但验证失败问题
2020/01/16 Javascript
extjs图表绘制之条形图实现方法分析
2020/03/06 Javascript
JavaScript中arguments的使用方法详解
2020/12/20 Javascript
Python程序设计入门(5)类的使用简介
2014/06/16 Python
Python实现获取某天是某个月中的第几周
2015/02/11 Python
浅谈Django学习migrate和makemigrations的差别
2018/01/18 Python
Python 3.x 安装opencv+opencv_contrib的操作方法
2018/04/02 Python
python实现随机梯度下降法
2020/03/24 Python
浅析PyCharm 的初始设置(知道)
2020/10/12 Python
pycharm 实现调试窗口恢复
2021/02/05 Python
高清屏中使用Canvas绘图出现模糊的问题及解决方法
2019/06/03 HTML / CSS
Tory Burch美国官方网站:美国时尚生活品牌
2016/08/01 全球购物
英国领先的鞋类零售商:Shoe Zone
2018/12/13 全球购物
机电专业体育教师求职信
2013/09/21 职场文书
公司人力资源的自我评价
2014/01/02 职场文书
《骑牛比赛》教后反思
2014/04/22 职场文书
销售区域经理岗位职责
2015/04/10 职场文书
在HTML5 localStorage中存储对象的示例代码
2021/04/21 Javascript
Python办公自动化之Excel(中)
2021/05/24 Python
MySQL的Query Cache图文详解
2021/07/01 MySQL