使用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遍历文件夹并删除特定格式文件的示例
Mar 05 Python
python单线程实现多个定时器示例
Mar 30 Python
python使用cStringIO实现临时内存文件访问的方法
Mar 26 Python
Python中关于字符串对象的一些基础知识
Apr 08 Python
python利用拉链法实现字典方法示例
Mar 25 Python
python urllib爬取百度云连接的实例代码
Jun 19 Python
python使用SMTP发送qq或sina邮件
Oct 21 Python
Python实现的绘制三维双螺旋线图形功能示例
Jun 23 Python
python获取当前文件路径以及父文件路径的方法
Jul 10 Python
python类中super() 的使用解析
Dec 19 Python
python实现与redis交互操作详解
Apr 21 Python
Python爬虫框架之Scrapy中Spider的用法
Jun 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
《心理测量者3》剧场版动画预告
2020/03/02 日漫
推荐5款跨平台的PHP编辑器
2014/12/25 PHP
php检测图片主要颜色的方法
2015/07/01 PHP
Laravel框架中集成MongoDB和使用详解
2019/10/17 PHP
新闻内页-JS分页
2006/06/07 Javascript
Flash+XML滚动新闻代码 无图片 附源码下载
2007/11/22 Javascript
推荐17个优美新鲜的jQuery的工具提示插件
2012/09/14 Javascript
JS实现判断滚动条滚到页面底部并执行事件的方法
2014/12/18 Javascript
AngularJS 入门教程之事件处理器详解
2016/08/19 Javascript
关于json字符串与实体之间的严格验证代码
2016/11/10 Javascript
jQuery扩展_动力节点Java学院整理
2017/07/05 jQuery
jQuery实现简单的Ajax调用功能示例
2019/02/15 jQuery
JS实现星星海特效
2019/12/24 Javascript
JS图片预加载三种实现方法解析
2020/05/08 Javascript
[01:25]DOTA2超级联赛专访iG 将调整状态找回自己
2013/06/05 DOTA
学习python的几条建议分享
2013/02/10 Python
Python库urllib与urllib2主要区别分析
2014/07/13 Python
python中的reduce内建函数使用方法指南
2014/08/31 Python
python中ConfigParse模块的用法
2014/09/29 Python
python命令行参数用法实例分析
2019/06/25 Python
django 自定义过滤器(filter)处理较为复杂的变量方法
2019/08/12 Python
python getpass实现密文实例详解
2019/09/24 Python
泰国第一在线超市:Tops
2021/02/13 全球购物
如何反序的迭代一个序列?how do I iterate over a sequence in reverse order
2012/02/04 面试题
城市轨道专业个人求职信范文
2013/09/23 职场文书
央视元宵晚会主持串词
2014/03/25 职场文书
淘宝客服工作职责
2014/07/11 职场文书
光学与应用专业毕业生求职信
2014/09/01 职场文书
银行党员批评与自我批评
2014/10/15 职场文书
后勤个人工作总结
2015/02/28 职场文书
升职自荐信怎么写
2015/03/05 职场文书
物业保安辞职信
2015/05/12 职场文书
导游词之河姆渡遗址博物馆
2019/10/10 职场文书
PHP 对接美团大众点评团购券(门票)的开发步骤
2021/04/03 PHP
golang 定时任务方面time.Sleep和time.Tick的优劣对比分析
2021/05/05 Golang
基于Python实现对比Exce的工具
2022/04/07 Python