使用ProcessPoolExecutor在执行程序中运行AsyncIO

编程入门 行业动态 更新时间:2024-10-27 03:42:14
本文介绍了使用ProcessPoolExecutor在执行程序中运行AsyncIO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我尝试使用ProcessPoolExecutor将阻止任务和非阻止(绑定I/O的)任务组合在一起,发现它的行为非常出乎意料.

I tried to combine blocking tasks and non-blocking (I/O bound) tasks using ProcessPoolExecutor and found it's behavior pretty unexpected.

class BlockingQueueListener(BaseBlockingListener): def run(self): # Continioulsy listening a queue blocking_listen() class NonBlockingListener(BaseNonBlocking): def non_blocking_listen(self): while True: await self.get_message() def run(blocking): blocking.run() if __name__ == "__main__": loop = asyncio.get_event_loop() executor = ProcessPoolExecutor() blocking = BlockingQueueListener() non_blocking = NonBlockingListener() future = loop.run_in_executor(executor, run(blocking)) loop.run_until_complete( asyncio.gather( non_blocking.main(), future ) )

我期望这两个任务将同时具有控制权,但是阻止任务以ProcessPoolExecutor块开始,并且永远不会返回控制权.怎么会这样在多处理执行器中,将常规协程和期货结合起来的正确方法是什么?

I was expecting that both tasks will have control concurrently, but blocking task started in ProcessPoolExecutor blocks and never return control. How could it happen? What the proper way to combine normal coroutines and futures started in multiprocessing executor?

推荐答案

此行:

future = loop.run_in_executor(executor, run(blocking))

实际上将运行阻塞函数并将其结果提供给执行程序.

Will actually run the blocking function and give its result to the executor.

根据文档,您需要显式地传递该函数以及其参数.

According to the documentation, you need to pass the function explicitly followed by its arguments.

future = loop.run_in_executor(executor, run, blocking)

更多推荐

使用ProcessPoolExecutor在执行程序中运行AsyncIO

本文发布于:2023-11-23 09:52:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621009.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:程序   ProcessPoolExecutor   AsyncIO

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!