我制作了一个应用程序,它等待任务很快运行,但不等待获取结果,但是有时我会注意到
asyncio.run_coroutine_threadsafe
确实跳过了一些任务,并且没有顺序地随机运行。
我将其与覆盖有uwsgi的烧瓶整合在一起(进程= 1,线程= 10)
run_coroutine_threadsafe
可以同时被任何线程调用
# get asyncio event loop
loop = asyncio.get_event_loop()
# define function to run in a thread
def InitAsyncio(loop):
asyncio.set_event_loop(loop)
loop.run_forever()
# create asyncio to run in thread
t = threading.Thread(target=InitAsyncio, args=(loop,))
t.start()
async def ProcessPicture(InputInt):
try:
print("This is a book. %d" % InputInt)
return
except:
return
def main():
for i in range(1000):
asyncio.run_coroutine_threadsafe(ProcessPicture(i), loop)
如何确保asyncio.run_coroutine_threadsafe
始终以不跳过的方式运行并命令任务运行