如何对 async 函数开启子线程执行呢
async some_callback(args):
await some_function()
需要将其作为目标线程 比如:
_thread = threading.Thread(target=some_callback, args=("some text"))
_thread.start()
但是 async函数 不能直接放到target里
如何实现上面的需求呢
方法1
async def some_callback(args):
await some_function()
def between_callback(args):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(some_callback(args))
loop.close()
_thread = threading.Thread(target=between_callback, args=("some text"))
_thread.start()
方法2
_thread = threading.Thread(target=asyncio.run, args=(some_callback("some text"),))
_thread.start()
网友评论