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

作者:小橘子Pythoner 时间:2023-12-12 04:16:05 

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

网上的建议多为通过调用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

标签:APScheduler3.0.1,定时任务
0
投稿

猜你喜欢

  • 简单介绍Python的Django框架加载模版的方式

    2022-09-17 16:49:31
  • python调用百度地图WEB服务API获取地点对应坐标值

    2021-06-05 18:21:28
  • Python基于traceback模块获取异常信息

    2022-04-27 11:30:10
  • Python中Tkinter组件Menu的具体使用

    2023-09-11 12:46:26
  • asp如何最大限度地实现安全登录功能?

    2010-07-11 21:11:00
  • C#如何在窗体程序中操作数据库数据

    2024-01-22 13:31:41
  • Python读取HTML中的canvas并且以图片形式存入Word文档

    2022-09-03 04:50:33
  • 首页访问感受提升三步曲

    2007-12-13 20:36:00
  • 在ASP中使用SQL语句之1:SELECT 语句

    2007-08-11 12:18:00
  • 如何利用Image Data Type在主页中显示图形?

    2010-01-01 15:13:00
  • 利用python实现万年历的查询

    2023-02-24 03:16:01
  • python保存文件方法小结

    2021-09-09 04:13:59
  • JavaScript+Node.js写一款markdown解析器

    2024-04-18 09:36:06
  • SQLServer2008新实例远程数据库链接问题(sp_addlinkedserver)

    2024-01-19 23:44:22
  • asp利用XmlHttp和Adodb.Stream采集图片

    2007-12-06 18:42:00
  • 微信小程序如何调用图片接口API并居中显示

    2023-08-09 15:05:30
  • Linux中将txt导入到mysql的方法教程

    2024-01-12 13:04:27
  • Oracle 数据库操作技巧集

    2010-07-26 12:49:00
  • Linux+Nginx+MySQL下配置论坛程序Discuz的基本教程

    2024-05-11 10:46:59
  • python自制简易mysql连接池的实现示例

    2023-04-14 20:23:55
  • asp之家 网络编程 m.aspxhome.com