使用asyncio.run时,如何将线程池执行程序提交到事件循环?

在Python documentation中,它指出:

  

应用程序开发人员通常应使用高级异步   函数,例如asyncio.run(),并且很少需要引用   循环对象或调用其方法。

     

还考虑使用asyncio.run()函数,而不要使用更低的   级别的功能来手动创建和关闭事件循环。

如果我需要使用asyncioThreadPoolExecutor,如何将执行程序提交到事件循环?

通常您可以这样做:

   # Create a limited thread pool.
    executor = concurrent.futures.ThreadPoolExecutor(
        max_workers=3,)

    event_loop = asyncio.get_event_loop()
    try:
        event_loop.run_until_complete(
            run_blocking_tasks(executor)
        )
    finally:
        event_loop.close()
lbanzg99 回答:使用asyncio.run时,如何将线程池执行程序提交到事件循环?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2973012.html

大家都在问