使用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开发的HTTP库requests详解
Aug 29 Python
Python cookbook(数据结构与算法)通过公共键对字典列表排序算法示例
Mar 15 Python
利用python实现微信头像加红色数字功能
Mar 26 Python
解决Python中list里的中文输出到html模板里的问题
Dec 17 Python
python实现雪花飘落效果实例讲解
Jun 18 Python
Python实现把类当做字典来访问
Dec 16 Python
Keras使用tensorboard显示训练过程的实例
Feb 15 Python
Python开发企业微信机器人每天定时发消息实例
Mar 17 Python
python numpy实现rolling滚动案例
Jun 08 Python
零基础学Python之前需要学c语言吗
Jul 21 Python
如何利用pycharm进行代码更新比较
Nov 04 Python
Python中glob库实现文件名的匹配
Jun 18 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中调用JAVA
2006/10/09 PHP
PHP初学者常见问题集合 修正版(21问答)
2010/03/23 PHP
如何用php获取文件名后缀
2013/06/09 PHP
shell脚本作为保证PHP脚本不挂掉的守护进程实例分享
2013/07/15 PHP
php采用curl模仿登录人人网发布动态的方法
2014/11/07 PHP
PHP获取QQ达人QQ信息的方法
2015/03/05 PHP
CodeIgniter自定义控制器MY_Controller用法分析
2016/01/20 PHP
php中的常用魔术方法汇总
2016/02/14 PHP
php执行多个存储过程的方法【基于thinkPHP】
2016/11/08 PHP
PHP实现的权重算法示例【可用于游戏根据权限来随机物品】
2019/02/15 PHP
Javascript模块化编程(一)模块的写法最佳实践
2013/01/17 Javascript
Jquery网页出现的乱码问题的三种解决方法
2013/06/30 Javascript
Javascript 赋值机制详解
2014/11/23 Javascript
Vue2.0父子组件传递函数的教程详解
2017/10/16 Javascript
JS实现网页抢购功能(触发,终止脚本)
2017/11/27 Javascript
浅谈Vuex注入Vue生命周期的过程
2019/05/20 Javascript
使用Promise封装小程序wx.request的实现方法
2019/11/13 Javascript
[56:01]2018DOTA2亚洲邀请赛 3.31 小组赛 B组 Effect vs EG
2018/03/31 DOTA
在Python的Django框架中获取单个对象数据的简单方法
2015/07/17 Python
使用apidocJs快速生成在线文档的实例讲解
2018/02/07 Python
Python File readlines() 使用方法
2018/03/19 Python
Python使用 Beanstalkd 做异步任务处理的方法
2018/04/24 Python
Python后台开发Django会话控制的实现
2019/04/15 Python
python Pandas如何对数据集随机抽样
2019/07/29 Python
django+echart数据动态显示的例子
2019/08/12 Python
Python中类似于jquery的pyquery库用法分析
2019/12/02 Python
python 字段拆分详解
2019/12/17 Python
django-csrf使用和禁用方式
2020/03/13 Python
keras的siamese(孪生网络)实现案例
2020/06/12 Python
html5使用Canvas绘图的使用方法
2017/11/21 HTML / CSS
有趣的睡衣和礼物:LazyOne
2019/11/27 全球购物
介绍下Java中==和equals的区别
2013/09/01 面试题
初三化学教学反思
2014/01/23 职场文书
2014年党课学习材料
2014/05/11 职场文书
负责培养人意见
2015/06/05 职场文书
Redis实现订单自动过期功能的示例代码
2021/05/08 Redis