Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Python apscheduler run job at 9,11,14,17,18 clock everyday

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?

like image 914
lqhcpsgbl Avatar asked Dec 04 '22 02:12

lqhcpsgbl


1 Answers

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.

like image 80
Alex Grönholm Avatar answered May 17 '23 09:05

Alex Grönholm