I am creating 2 background schedulers, after the tasks on both the schedulers are started, I am calling scheduler.shutdown(wait=True) but it is not waiting for the tasks to complete, I got around this by having a while loop which check pending jobs using scheduler.get_jobs(), but reading the documentation it should not be required because shutdown(wait=True) is for this purpose, or did I read it wrong.
import time
import os
import random
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.executors.pool import ThreadPoolExecutor
from datetime import datetime,timezone,timedelta
from zoneinfo import ZoneInfo
some_data = None # task1() updates it and task2() reads it
def time_now():
return datetime.now(timezone.utc).astimezone(ZoneInfo('localtime'))
def time_now_str():
return time_now().strftime('%y-%m-%d %H:%M:%S.%f')
def random_int():
return random.randint(10000000000, 99999999999)
def task1(taskid):
global some_data
os.system('/usr/bin/dd if=/dev/zero of=/dev/null bs=10 count=10000 2> /dev/null') ## dummy task
some_data =random_int()
print(f'## Task1 ID: {taskid}, Generated some random data = {some_data}, '+time_now_str())
return some_data
def task2(taskid):
global some_data
if some_data is not None:
curr_data = some_data
print(f'-- Task2 ID: {taskid}, STARTED task2 for data = {curr_data}: '+time_now_str())
os.system('/usr/bin/dd if=/dev/zero of=/dev/null bs=10 count=5000000 2> /dev/null') ## dummy task
print(f'-- Task2 ID: {taskid}, DONE task2 for data = {curr_data}: '+time_now_str())
else:
print(f'-- Task2 ID: {taskid}, WARNING, no data yet, looks like task1() has not run yet, '+time_now_str())
## main()
task1_scheduler = BackgroundScheduler(executors={'default': ThreadPoolExecutor(1)}, job_defaults={'misfire_grace_time': 500, 'coalesce': False})
task2_scheduler = BackgroundScheduler(executors={'default': ThreadPoolExecutor(1)}, job_defaults={'misfire_grace_time': 500, 'coalesce': False})
start_time = time_now()
print('Start Time: '+start_time.strftime('%y-%m-%d %H:%M:%S.%f'))
task1_tm = start_time
for idx, interval in enumerate( [1, 8, 14], start=1):
task1_id = 'task1_id_'+str(idx)
task1_tm += timedelta(seconds=interval)
task2_scheduler.add_job(task1,'date', run_date=task1_tm, args=[task1_id], id=task1_id, max_instances=1)
task2_tm=start_time
for idx, interval in enumerate([0, 4,19,28], start=1):
task2_id = 'task2_id_'+str(idx)
task2_tm += timedelta(seconds=interval)
task2_scheduler.add_job(task2,'date', run_date=task2_tm, args=[task2_id], id =task2_id, max_instances=1)
task1_scheduler.start()
task2_scheduler.start()
# I should not need this
while len(task1_scheduler.get_jobs()) > 0 or len(task2_scheduler.get_jobs()) > 0:
time.sleep(1)
task1_scheduler.shutdown(wait=True)
task2_scheduler.shutdown(wait=True)
print(f'Tasks completed: {time_now_str()}n')
OS: Linux Mint 21.2
APScheduler: v3.10.4
Python: v3.10.12
Rajiv Kumar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.