使用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 元类使用说明
Dec 18 Python
python中关于日期时间处理的问答集锦
Mar 08 Python
Python功能键的读取方法
May 28 Python
python实现简单ftp客户端的方法
Jun 28 Python
实例讲解Python设计模式编程之工厂方法模式的使用
Mar 02 Python
深入理解python try异常处理机制
Jun 01 Python
学习Python3 Dlib19.7进行人脸面部识别
Jan 24 Python
pandas的唯一值、值计数以及成员资格的示例
Jul 25 Python
如何提高python 中for循环的效率
Apr 15 Python
python模拟实现分发扑克牌
Apr 22 Python
详解python 支持向量机(SVM)算法
Sep 18 Python
Python提取PDF指定内容并生成新文件
Jun 09 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
用mysql内存表来代替php session的类
2009/02/01 PHP
PHP多个版本的分析解释
2011/07/21 PHP
PHP新手NOTICE错误常见解决方法
2011/12/07 PHP
自定义php类(查找/修改)xml文档
2013/03/26 PHP
php中数字0和空值的区别分析
2014/06/05 PHP
DEDECMS首页调用图片集里的多张图片
2015/06/05 PHP
PHP加密解密实例分析
2015/12/25 PHP
PHP微信PC二维码登陆的实现思路
2017/07/13 PHP
showModelessDialog()使用详解
2006/09/07 Javascript
Whatever:hover 无需javascript让IE支持丰富伪类
2010/06/29 Javascript
IE6浏览器中window.location.href无效的解决方法
2014/11/20 Javascript
如何根据百度地图计算出两地之间的驾驶距离(两种语言js和C#)
2015/10/29 Javascript
jQuery mobile类库使用时加载导航历史的方法简介
2015/12/04 Javascript
js控制文本框只能输入中文、英文、数字与指定特殊符号的实现代码
2016/09/09 Javascript
ajax级联菜单实现方法实例分析
2016/11/28 Javascript
微信小程序 switch组件详解及简单实例
2017/01/10 Javascript
详解用node-images 打造简易图片服务器
2017/05/08 Javascript
利用vue和element-ui设置表格内容分页的实例
2018/03/02 Javascript
Javascript读取上传文件内容/类型/字节数
2019/04/30 Javascript
[37:02]OG vs INfamous 2019国际邀请赛小组赛 BO2 第二场 8.15
2019/08/17 DOTA
详解Python设计模式编程中观察者模式与策略模式的运用
2016/03/02 Python
python实现对指定输入的字符串逆序输出的6种方法
2018/04/26 Python
python学习开发mock接口
2019/04/28 Python
Django中如何使用sass的方法步骤
2019/07/09 Python
Python OrderedDict的使用案例解析
2019/10/25 Python
TensorFlow索引与切片的实现方法
2019/11/20 Python
python实现126邮箱发送邮件
2020/05/20 Python
加拿大鞋网:Globo Shoes
2019/12/26 全球购物
差生评语大全
2014/05/04 职场文书
公司放假通知怎么写
2015/04/15 职场文书
入党介绍人意见2015
2015/06/01 职场文书
病假条格式范文
2015/08/17 职场文书
积极心理学课程心得体会
2016/01/22 职场文书
创业计划书之情侣餐厅
2019/09/29 职场文书
js中Map和Set的用法及区别实例详解
2022/02/15 Javascript
MySQL约束(创建表时的各种条件说明)
2022/06/21 MySQL