使用APScheduler3.0.1 实现定时任务的方法

yipeiwu_com6年前Python基础

需求是在某一指定的时刻执行操作

网上的建议多为通过调用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 django搭建layui提交表单,表格,图标的实例

Python django搭建layui提交表单,表格,图标的实例

利用layui制作与众不同的感谢表单,表格 layui极大的提高了前端开发效率,它极具个性的样式等等都非常吸引人,接下来我将为大家展示如何利用Python的django框架与layui制...

Python3.5面向对象程序设计之类的继承和多态详解

Python3.5面向对象程序设计之类的继承和多态详解

本文实例讲述了Python3.5面向对象程序设计之类的继承和多态。分享给大家供大家参考,具体如下: 1、继承的定义 继承是指:可以使用现有类的所有功能,并在无需重新编写原来的类的情况下...

Python 读取某个目录下所有的文件实例

在处理数据的时候,因为没有及时的去重,所以需要重新对生成txt进行去重。 可是一个文件夹下有很多txt,总不可能一个一个去操作,这样效率太低了。这里我们需要用到 os 这个包 关键的代码...

利用Python代码实现数据可视化的5种方法详解

利用Python代码实现数据可视化的5种方法详解

前言 数据科学家并不逊色于艺术家。他们用数据可视化的方式绘画,试图展现数据内隐藏的模式或表达对数据的见解。更有趣的是,一旦接触到任何可视化的内容、数据时,人类会有更强烈的知觉、认知和交流...

在Python中增加和插入元素的示例

在Python中append 用来向 list 的末尾追加单个元素,如果增加的元素是一个list,那么这个list将作为一个整体进行追加。 例如: Python代码 li=['a',...