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

yipeiwu_com5年前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函数装饰器实现方法详解

本文实例讲述了Python函数装饰器实现方法。分享给大家供大家参考,具体如下: 编写函数装饰器 这里主要介绍编写函数装饰器的相关内容。 跟踪调用 如下代码定义并应用一个函数装饰器,来统计...

详解pandas如何去掉、过滤数据集中的某些值或者某些行?

详解pandas如何去掉、过滤数据集中的某些值或者某些行?

摘要在进行数据分析与清理中,我们可能常常需要在数据集中去掉某些异常值。具体来说,看看下面的例子。 0.导入我们需要使用的包 import pandas as pd pandas是很常...

Python与人工神经网络:使用神经网络识别手写图像介绍

Python与人工神经网络:使用神经网络识别手写图像介绍

人体的视觉系统是一个相当神奇的存在,对于下面的一串手写图像,可以毫不费力的识别出他们是504192,轻松到让人都忘记了其实这是一个复杂的工作。 实际上在我们的大脑的左脑和右脑的皮层都有...

Python Numpy计算各类距离的方法

Python Numpy计算各类距离的方法

详细: 1.闵可夫斯基距离(Minkowski Distance) 2.欧氏距离(Euclidean Distance) 3.曼哈顿距离(Manhattan Distance) 4.切比...

将pip源更换到国内镜像的详细步骤

将pip源更换到国内镜像的详细步骤

pip是一个很好用的第三方库安装方式,但是默认的源没法连接,就算有时候可以成功率也很低,所以换成国内镜像源比较方便。 将pip源更换到国内镜像 用pip管理工具安装库文件时,默认使用国...