8

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()
Amine Harbaoui
  • 1,247
  • 2
  • 17
  • 34

1 Answers1

5

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) 
nik
  • 118
  • 4
Amine Harbaoui
  • 1,247
  • 2
  • 17
  • 34