首页 > 解决方案 > 如何在不阻塞整个程序的情况下使函数在后台运行?

问题描述

在不阻塞整个程序的情况下使该函数在后台运行时遇到一些困难,如何在不阻塞整个程序的情况下循环运行该函数?这是函数: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")

标签: pythonpython-3.x

解决方案


线程是您正在寻找的。考虑以下代码:

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()

希望我有帮助!:)


推荐阅读