Amine Harbaoui
Amine Harbaoui

Reputation: 1295

Run schedule function in new thread

I used the schedule library to schedule a function every X seconds:

Want I want is to run this function on separate thread. I found this in the documentation on how to Run the scheduler in a separate thread but I didn't understand what he did.

Is there someone who can explain to me how to do that ?

Update:

This what I tried:

def post_to_db_in_new_thread():
    schedule.every(15).seconds.do(save_db)

t1 = threading.Thread(target=post_to_db_in_new_thread, args=[])

t1.start()

Upvotes: 9

Views: 3748

Answers (1)

Amine Harbaoui
Amine Harbaoui

Reputation: 1295

You don't really need to update schedule in every task

import threading
import time
import schedule 


def run_threaded(job_func):
    job_thread = threading.Thread(target=job_func)
    job_thread.start()



schedule.every(15).seconds.do(run_threaded, save_db)
while 1:
    schedule.run_pending()
    time.sleep(1) 

Upvotes: 6

Related Questions