【问题标题】:Run schedule function in new thread在新线程中运行调度函数
【发布时间】:2019-02-02 00:52:45
【问题描述】:

我使用schedule 库每 X 秒调度一个函数:

我想要的是在单独的线程上运行这个函数。我在有关如何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()

【问题讨论】:

  • 向我们展示您的代码,到目前为止您尝试过什么?
  • 问题已更新

标签: python python-3.x schedule


【解决方案1】:

您实际上并不需要在每个任务中更新计划

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) 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-26
    • 2020-10-17
    • 1970-01-01
    • 2023-04-09
    • 2013-03-16
    • 2021-08-29
    • 2012-05-08
    • 2018-01-21
    相关资源
    最近更新 更多