I am working on a period task with Python apscheduler, I want the code execute on 9:00, 11:00, 16:00, 17:00 every day and here is an example code for the job:
#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()
It works, but repeat four times code seems silly, so my problem is how to make the code short to set the function run at 9:00, 11:00, 16:00, 17:00 every day?
Why would you want to schedule the job four times separately when the documentation gives clear examples of how to do it properly?
@sched.scheduled_job('cron', hour='9,11,16,17')
def timed_job():
print ctime()
See this and this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With