I have a script for a Raspberry Pi, which should send reports two times a day at 8:10 and at 20:10 according to a schedule. But usually the task (send data function) starts from 2-10 minutes late from the time scheduled, though I use multiply schedulers. Sometimes the tasks start at accurate time. It would be very interesting to find out why.
I understand that when the schedulers are executed sequentially, their work can affect each other. But in this case I run each of them in a separate thread.
The schedule associated part of my code:
main.py
import time
import threading
import schedule
def send_data():
print('Task started at ', dt.datetime.now())
# Here is a data sending code
# ...
def thr_pending_1():
while True:
scheduler1.run_pending()
time.sleep(1)
def thr_pending_2():
while True:
scheduler2.run_pending()
time.sleep(1)
if __name__ == '__main__':
# Some code
# ...
scheduler1 = schedule.Scheduler()
scheduler1.every().day.at('08:10').do(send_data)
scheduler2 = schedule.Scheduler()
scheduler2.every().day.at('20:10').do(send_data)
threading.Thread(target=thr_pending_1, daemon=True, name='Morning report').start()
threading.Thread(target=thr_pending_2, daemon=True, name='Evening report').start()