2

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()
Ivan
  • 121
  • 1
  • 9
  • 1
    Maybe it could we useful for somebody. We looked at the implementation of schedule package. What they do is whenever you set a scheduler with every().day and set a time, the first time the call will be called on time. Then after your callback finished, in my case the callback is send_data, schedule will set the next call to current time + 24h. But if send_data takes 2min to execute, the current time will be equal to the target time + 2min. So schedule will call the callback the next day with 2min delay. It means also that this is cumulative. – Ivan Feb 16 '22 at 08:59

0 Answers0