dbader / schedule

Python job scheduling for humans.
https://schedule.readthedocs.io/
MIT License
11.73k stars 959 forks source link

Multiply schedulers run tasks later than scheduled #508

Open IvanOsyanin opened 2 years ago

IvanOsyanin commented 2 years ago

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()
IvanOsyanin commented 2 years ago

I've tested the work of this code more carefully, it turned out that the delay occurs when the script sends the morning report (scheduler1, thr_pending_1)

pedrofaria09 commented 1 year ago

i also have a similar problem, what is the workaround for it? Thanks.