使用APScheduler3.0.1 实现定时任务的方法
作者:小橘子Pythoner 发布时间:2023-12-12 04:16:05
标签: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')
来源:https://blog.csdn.net/lrz4119/article/details/42742909


猜你喜欢
- 今天在工作中遇到了一个问题,需要按时间查询,可是查询出来的结果显示的不正确。举个例子来说,要查找出2007-10-12至2007-10-31
- 前言关于 var、let 和 const 三个关键字的区别,是一个老生常谈的问题,也是经典的面试题。本篇文章将全面讲解三者的特性,以及它们之
- 本篇介绍Python字典的常见操作。修改字典元素,如图。添加字典元素,如图。删除字典元素del方法,如图。删除字典元素clear方法,如图。
- 一、区别1、 history和hash都是利用浏览器的两种特性实现前端路由,history是利用浏览历史记录栈的API实现,hash是监听l
- 平时写得多的是python,最近看了一点go,今天碰到了一个问题,和大家分享一下package mainimport "fmt&q
- 通过对四则运算的学习,已经初步接触了Python中内容,如果看官是零基础的学习者,可能有点迷惑了。难道在IDE里面敲几个命令,然后看到结果,
- 接口在 Go 语言中,接口是一种抽象的类型,是一组方法的集合。接口存在的目的是定义规范,而规范的细节由其他对象去实现。我们来看一个例子:im
- 一键执行虚拟机一键安装python3.8环境,只需将网络适配器改为nat模式即可(确保主机能够上网),随后将tar包放入/root目录下,执
- 各大著名厂家、公司的banner广告设计欣赏,尺寸468x60,gif格式!有acer,阿尔卡特,AMD,中国电信,爱立信,Greatwal
- MYSQL TIMESTAMP字段进行时间加减运算在数据分析过程中,想当然地对TIMESTAMP字段进行运算,导致结果谬之千里计算公式如下-
- 解决办法有很多: 1 . select * from 表 where
- pytorch中的权值初始化官方论坛对weight-initilzation的讨论torch.nn.Module.apply(fn)torc
- 一、mariadb与mysql简介1、mariadb简介MariaDB由MySQL的创始人Michael Widenius(英语:Micha
- 我们常常会用到PHP过滤一些标签的功能,比如过滤链接标签、过滤script标签等等,下面就介绍一下PHP过滤常用标签的正则表达式代码:$st
- 数据库的操作是当前系统开发必不可少的开发部分之一,尤其是在现在的大数据时代,数据库尤为重要。但是你真的懂得Java与数据库是怎么连接的么?先
- 前言最近很多新入职的同事问这个问题,特别是通过 homebrew 自动安装的 mysql ,其版本默认已经是 8.0 了,由于增加了一些安全
- 首先,来说一下对话框: 对话框在Windows应用程序中使用非常普遍,许多应用程序的设定,与用户交互需要通过对话框来进行,因此对话框是Win
- 在Django model中对一张表的几个字段进行联合约束和联合索引,例如在购物车表中,登录的用户和商品两个字段在一起表示唯一记录。举个栗子
- 虽然现在的GMail已经看不到这个效果,但之前那个可爱的深红色Loading仍然让许多人喜爱。我也将这款效果融入了我自己的WordPress
- 前言使用python直接使用pip install xx时,出现 Could not fetch URL https://pypi.pyth