APScheduler 选项

APScheduler options

我正在尝试使用 Advace Python 调度程序以编程方式安排一些作业,我的问题是在文档中只提到了如何使用 'interval' 触发器类型进行安排,那么 'cron' 和 'date'。有没有关于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)

我如何根据 'date' 或 'cron'

安排时间

我使用的是最新的 APScheduler 版本 3.0.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
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 作业。