我正在使用 Python apscheduler 执行周期性任务,我希望代码每天在 9:00、11:00、16:00、17:00 执行,这里是该作业的示例代码:
#coding=utf-8
from apscheduler.schedulers.blocking import BlockingScheduler
import logging
logging.basicConfig()
from time import ctime
sched = BlockingScheduler()
@sched.scheduled_job('cron', hour=16)
def timed_job_one():
print "16"
print ctime()
@sched.scheduled_job('cron', hour=17)
def timed_job_one():
print "17"
print ctime()
@sched.scheduled_job('cron', hour=9)
def timed_job_two():
print ctime()
print '9'
@sched.scheduled_job('cron', hour=11)
def timed_job_two():
print ctime()
print '11'
sched.start()
它有效,但重复四次代码似乎很愚蠢,所以我的问题是如何使代码简短以设置函数在每天 9:00、11:00、16:00、17:00 运行?
最佳答案
既然文档提供了如何正确执行任务的清晰示例,您为什么要分别安排四次作业?
@sched.scheduled_job('cron', hour='9,11,16,17')
def timed_job():
print ctime()
关于python - 如何设置 Python apscheduler 在每天 9、11、14、17、18 时钟运行作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32221118/