python - 如何在不阻塞整个程序的情况下使函数在后台运行?
问题描述
在不阻塞整个程序的情况下使该函数在后台运行时遇到一些困难,如何在不阻塞整个程序的情况下循环运行该函数?这是函数:while True: schedule.run_pending()
感谢您的任何回复。
编辑:
def FunctioninLoop():
while True:
schedule.run_pending()
async def MyFunction():
ScheduleToexecute=schedule.every().minute.do(Functionscheduled)
t = Thread(target=FunctioninLoop())
t.start()
print("The execution is going on")
解决方案
线程是您正在寻找的。考虑以下代码:
from threading import Thread
def myfunc(a, b, c):
pass
# Creates a thread
t = Thread(target=myfunc, args=(1, 2, 3))
# Launches the thread (while not blocking the main execution)
t.start()
somecode
somecode
somecode
# Waits for the thread to return (not a must)
t.join()
希望我有帮助!:)
推荐阅读
- reactjs - 单击外部的全局事件处理程序可防止触发反应事件
- r - 根据另一个数据框中的列范围对一个数据框中的值进行分组
- javascript - npm start throwing error status 1 require is not a function
- javascript - 通过 Javascript 更改类的不透明度
- python - `if i != _i and j != _j and k != _k:` 继续但报告第一次出现?
- python - Tweepy:从 Twitter API (created_at) 检索推文属性的问题
- c# - 第一个表格不关闭,第二个不打开
- javascript - Teams 中的“Promise.prototype.finally() 不是函数”错误
- asp.net-mvc - 使用 GetWebLoginClientContext 时使用 SharePoint Online 进行身份验证时出现问题
- python - 如何从长度不等的列表中创建假人数据框?