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

 更新时间:2019年07月22日 08:54:30   作者:小橘子Pythoner  
今天小编就为大家分享一篇使用APScheduler3.0.1 实现定时任务的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

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

网上的建议多为通过调用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 Opencv实现停车位识别思路详解

    python Opencv实现停车位识别思路详解

    这篇文章主要介绍了Opencv实现停车位识别,本文通过示例代码场景分析给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-07-07
  • 利用Python操作消息队列RabbitMQ的方法教程

    利用Python操作消息队列RabbitMQ的方法教程

    RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统。他遵循Mozilla Public License开源协议。下面这篇文章主要给大家介绍了关于利用Python操作消息队列RabbitMQ的方法教程,需要的朋友可以参考下。
    2017-07-07
  • Python中实现常量(Const)功能

    Python中实现常量(Const)功能

    这篇文章主要介绍了Python中实现常量(Const)功能,python语言本身没有提供const,本文使用一个类来实现常量定义功能,并介绍了使用方法,需要的朋友可以参考下
    2015-01-01
  • Pycharm打印大数据文件显示不全的解决方法

    Pycharm打印大数据文件显示不全的解决方法

    这篇文章主要介绍了Pycharm打印大数据文件显示不全的解决方法,昨晚写了个小爬虫,简单分析下发现可以修改请求的url,直接获取所有目标的数据,想先打印在控制台看看,发现打印的数据不全,所以本文记录了一下解决方法,需要的朋友可以参考下
    2024-03-03
  • python requests库的使用

    python requests库的使用

    这篇文章主要介绍了python requests库的使用,帮助大家更好的利用python进行爬虫,感兴趣的朋友可以了解下
    2021-01-01
  • python中_init_.py的作用

    python中_init_.py的作用

    __init__.py的作用是告诉Python这是一个包,并且可以包含初始化操作、控制子模块导入、设置包级别变量和函数等,本文就来详细的介绍一下_init_.py的作用,感兴趣的可以了解一下
    2025-01-01
  • python 指定源路径来解决import问题的操作

    python 指定源路径来解决import问题的操作

    这篇文章主要介绍了python 指定源路径来解决import问题的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-03-03
  • 用Python的urllib库提交WEB表单

    用Python的urllib库提交WEB表单

    上次实现的校园网IP网关登录器其中一个关键部分就是提交登录网页的表单,下面是我的Python实现代码
    2009-02-02
  • 基于python元祖与字典与集合的粗浅认识

    基于python元祖与字典与集合的粗浅认识

    下面小编就为大家带来一篇基于python元祖与字典与集合的粗浅认识。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-08-08
  • Python的类成员变量默认初始值的坑及解决

    Python的类成员变量默认初始值的坑及解决

    这篇文章主要介绍了Python的类成员变量默认初始值的坑及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-02-02

最新评论