代码之家  ›  专栏  ›  技术社区  ›  Amine Harbaoui

在新线程中运行调度函数

  •  1
  • Amine Harbaoui  · 技术社区  · 6 年前

    我用了 schedule

    我想要的是在不同的线程上运行这个函数。我在关于如何 Run the scheduler in a separate thread 但我不明白他做了什么。

    更新 :

    这就是我所尝试的:

    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()
    
    1 回复  |  直到 6 年前
        1
  •  3
  •   Amine Harbaoui    6 年前

    所以我会回答我自己的问题。

    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)