R
R
RINAMI2021-08-12 18:28:35
Python
RINAMI, 2021-08-12 18:28:35

Moved the Schedule to the second thread, but why doesn't it work?

In general, there are "work" functions:

import threading
from threading import Thread
import time
import schedule
from datetime import datetime
def run_schedule():  # Kerindaily
    schedule.every().day.at('10:00').do(everyday)
    schedule.every().day.at('10:00').do(everydayphoto)
    schedule.every().day.at('21:14').do(everynight)
    schedule.every().day.at('21:14').do(everydayphoto)
    while True:
        schedule.run_pending()
        time.sleep(1)
        pass

And the flow itself:
def thread2():  # Поток 2
    thr2 = Thread(target=run_schedule)
    thr2.start()
    pass

Added at the end of the code:
if __name__ == '__main__':
    thread2()

The functions send a message and a photo to the chat, but the schedule does not work, no errors, nothing, and for some reason the time freezes in one place

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Asura, 2021-08-12
@RINAMI

It is not necessary to move the entire function to the second thread. You just need to take out the cycle and you can use schedule everywhere.

import threading
from threading import Thread
import time
import schedule
from datetime import datetime

def everyday():
    pass
def everydayphoto():
    pass
def everynight():
    pass
def everydayphoto():
    pass

schedule.every().day.at('10:00').do(everyday)
schedule.every().day.at('10:00').do(everydayphoto)
schedule.every().day.at('21:14').do(everynight)
schedule.every().day.at('21:14').do(everydayphoto)



def sched(num):
    while True:
        schedule.run_pending()
        time.sleep(num)

thr = Thread(target = sched, daemon=True, args = (1,))
thr.start()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question