使用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请求的方法小结
Jul 08 Python
python基础知识小结之集合
Nov 25 Python
快速排序的算法思想及Python版快速排序的实现示例
Jul 02 Python
利用Tkinter和matplotlib两种方式画饼状图的实例
Nov 06 Python
Python爬取十篇新闻统计TF-IDF
Jan 03 Python
Python自动化之数据驱动让你的脚本简洁10倍【推荐】
Jun 04 Python
详细整理python 字符串(str)与列表(list)以及数组(array)之间的转换方法
Aug 30 Python
使用Python脚本从文件读取数据代码实例
Jan 19 Python
解决python Jupyter不能导入外部包问题
Apr 15 Python
Python类成员继承重写的实现
Sep 16 Python
Pycharm Available Package无法显示/安装包的问题Error Loading Package List解决
Sep 18 Python
Python道路车道线检测的实现
Jun 27 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
简单采集了yahoo的一些数据
2007/02/14 PHP
php empty函数判断mysql表单是否为空
2010/04/12 PHP
php表单转换textarea换行符的方法
2010/09/10 PHP
ThinkPHP中URL路径访问与模块控制器之间的关系
2014/08/23 PHP
php制作简单模版引擎
2016/04/07 PHP
php 读写json文件及修改json的方法
2018/03/07 PHP
PHP简单实现解析xml为数组的方法
2018/05/02 PHP
php微信公众号开发之图片回复
2018/10/20 PHP
关于图片验证码设计的思考
2007/01/29 Javascript
js 匿名调用实现代码
2009/06/19 Javascript
jquery 模拟类搜索框自动完成搜索提示功能(改进)
2010/05/24 Javascript
Javascript基础教程之if条件语句
2015/01/18 Javascript
javascript实现的右下角弹窗实例
2015/04/24 Javascript
ajax图片上传,图片异步上传,更新实例
2016/12/30 Javascript
漂亮实用的页面loading(加载)封装代码
2017/02/03 Javascript
Bootstrap轮播图学习使用
2017/02/10 Javascript
chosen实现省市区三级联动
2018/08/16 Javascript
vue实现树形结构样式和功能的实例代码
2019/10/15 Javascript
[01:04:31]DOTA2-DPC中国联赛定级赛 iG vs Magma BO3第二场 1月8日
2021/03/11 DOTA
python开发利器之ulipad的使用实践
2017/03/16 Python
Python 多线程Threading初学教程
2017/08/22 Python
Python数据处理numpy.median的实例讲解
2018/04/02 Python
Python数据类型之Tuple元组实例详解
2019/05/08 Python
解决使用python print打印函数返回值多一个None的问题
2020/04/09 Python
详解python命令提示符窗口下如何运行python脚本
2020/09/11 Python
css3实现超立体3D图片侧翻倾斜效果
2014/04/16 HTML / CSS
德国运动鞋网上商店:Afew Store
2018/01/05 全球购物
泰国办公用品购物网站:OfficeMate
2018/02/04 全球购物
澳大利亚最好的厨具店:Kitchen Warehouse
2018/03/13 全球购物
法国房车租赁网站:Yescapa
2019/08/26 全球购物
大学毕业后的十年规划
2014/01/07 职场文书
2015迎新晚会开场白
2015/05/29 职场文书
电影建党伟业观后感
2015/06/01 职场文书
郭明义观后感
2015/06/08 职场文书
2019年手机市场的调研报告2篇
2019/10/10 职场文书
nginx配置限速限流基于内置模块
2022/05/02 Servers