【问题标题】:APScheduler optionsAPScheduler 选项
【发布时间】:2015-04-03 09:03:38
【问题描述】:

我正在尝试使用 Advace Python Scheduler 以编程方式安排一些作业,我的问题是在文档中只提到了如何使用“间隔”触发器类型进行安排,而“cron”和“日期”呢?有没有关于 APScheduler 调度选项的完整文档?

例如:

#!/usr/bin/env python

from time import sleep 
from apscheduler.scheduler import Scheduler

sched = Scheduler()
sched.start()        

# define the function that is to be executed
def my_job(text):
    print text

job = sched.add_job(my_job, 'interval', id='my_job', seconds=10, replace_existing=True, args=['job executed!!!!'])

while True:
        sleep(1)

我如何根据“日期”或“计划”安排时间

我正在使用最新的 APScheduler 3.0.2 版

谢谢

【问题讨论】:

    标签: python apscheduler


    【解决方案1】:
    sched.add_job(my_job, trigger='cron', hour='22', minute='30')
    

    表示每天 22:30 调用函数“my_job”一次。

    APScheduler 是个好东西,但是缺少文档,很可惜,你可以阅读源代码了解更多。

    还有一些提示:

    1. 使用 *

      sched.add_job(my_job, trigger='cron', second='*') # trigger every second.
      
    2. 更多属性

      {'year': '*', 'month': 1, 'day': 1, 'week': '*', 'day_of_week': '*', 'hour': 0, 'minute': 0, 'second': 0}
      

    在我看来,cron 作业在大多数情况下可以取代 date 作业。

    【讨论】:

    【解决方案2】:

    基于date

    job = sched.add_date_job(my_job, '2013-08-05 23:47:05', ['text']) # or can pass datetime object.
    

    例如

    import datetime
    from datetime import timedelta
    >>>job = sched.add_date_job(my_job, datetime.datetime.now()+timedelta(seconds=10), ['text'])
    'text' # after 10 seconds
    

    基于cron

    >>>job = sched.add_cron_job(my_job, second='*/5',args=['text'])
    'text' every 5 seconds
    

    另一个例子

    >>>job = sched.add_cron_job(my_job,day_of_week='mon-fri', hour=17,args=['text'])
    "text"  #This job is run every weekday at 5pm
    

    【讨论】:

    • 这对最新版本的调度程序不再有效
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-03
    • 1970-01-01
    • 1970-01-01
    • 2016-04-26
    • 2019-12-07
    • 2020-02-23
    • 1970-01-01
    相关资源
    最近更新 更多