gpt4 book ai didi

python - 在新线程中运行调度函数

转载 作者:行者123 更新时间:2023-12-03 16:51:57 26 4
gpt4 key购买 nike

我用了 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()

最佳答案

你真的不需要在每个任务中更新时间表

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)

关于python - 在新线程中运行调度函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52040070/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com