简体   繁体   中英

Schedule all jobs from the list

I have extracted a list from a database with a format like this:

task_list = [
("script_to_run.py", date(2019,8,12), time(10,20), "one time"),
("script2_to_run.py", date(2019,8,12), time(10,30), "daily"),
("script3_to_run.py", date(2019,8,12), time(10,40), "daily")]

Now in the for loop I'm trying to combine date and time to set a job for scheduling with APscheduler, but first I want to separate daily and one time jobs:


def send_jobs():
    for i in task_list:
        if i[3] =='one time':
            one_time_schedule(i)
        if i[3] =='daily':
             daily_schedule(i)

Now, the first task is "one time":

def one_time_schedule(row):
    date_time = datetime.combine(row[1],row[2])
    sched.add_jos(function, "date", run_date = date_time)

Then in the main:

sched = BackgroundSchedule()
send_jobs()
sched.start()

Problem is, for me, that program gets into the send_jobs method, identifies the first job, and sends it to the method one_time_scheduler() . There the job is added, but the program doesn't return to the next one, the second element in the list, so I finish the program with only one job added instead of many.

The result when printing is:

print(shed.get_jobs())
[<Job (id= 5213437jopf56423a name=function)>]

Can you try this:

from apscheduler.schedulers.background import BackgroundScheduler
from datetime import datetime, date, time

schedule = BackgroundScheduler()


def daily_job(): pass

def one_time_job(): pass


task_list = [
    ('script_to_run.py', date(2019, 8, 12), time(10, 20), 'one time'),
    ('script2_to_run.py', date(2019, 8, 12), time(10, 30), 'daily'),
    ('script3_to_run.py', date(2019, 8, 12), time(10, 40), 'daily')]

for script, task_date, task_time, frequency in task_list:
    if frequency == 'daily':
        run_date = datetime.combine(task_date, task_time)
        schedule.add_job(func=daily_job,
                         trigger='date',
                         run_date=run_date)
    else:       
        schedule.add_job(func=one_time_job,
                         trigger='cron',
                         hour=task_time.hour,
                         minute=task_time.minute)
schedule.start()

The output I'm getting is:

>> print(schedule.get_jobs())
[<Job (id=13028449c2a44b169fee37dfbacb2742 name=one_time_job)>,
 <Job (id=647e0e2c90ef4e35a3b03dbeec794197 name=daily_job)>,
 <Job (id=65d352d400f64287a80f5cb06fcb5f03 name=daily_job)>]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM