我注意到有时run\u coroutine\u threadsafe会跳过助教

2024-09-28 05:29:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经做了一个应用程序,它等待一个任务很快运行,但不等待得到结果,但我注意到有时

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始终以不跳过的方式运行并命令任务运行


Tags: toruninloopeventasynciogetdef

热门问题