竞争者
concurrent-utils的Python项目详细描述
python 3.7+asyncio的并发实用程序;主要部分是组件抽象。 为了支持这一点,一些用于任务间、线程间和进程间通信的管道实现 还提供了一些序列化实用程序。 进程间通信基于zeromq。 最后,提供了一个类EventLoopThread,可用于桥接同步和异步代码。
“组件”是独立执行的代码,如异步任务、线程、工作线程的负载或进程。 组件处理由其所有者发出的命令,并创建要由其所有者处理的事件。 组件也可能产生结果,当然也可能与所有者以外的其他实体通信。
尽管asyncio被大量使用,但工作负载与其所有者之间的连接将两者分离 允许任何并发模型。 下面是一个示例,取自并改编自测试套件:
import asyncio from concurrent.futures import ThreadPoolExecutor from concurrent_utils.component import Component, component_workload, start_component_in_thread async def test_thread_component_result_success_and_command(): @component_workload async def component(x, *, commands, events): await events.send(Component.EVENT_START) ### startup complete # reply to command await commands.send(await commands.recv() + 1) # return return x e = ThreadPoolExecutor(1) comp = await start_component_in_thread(e, component, 1) assert await comp.request(1) == 2 assert await comp.result() == 1 asyncio.run(test_thread_component_result_success_and_command())
尽管组件被定义为协程,但它(在本例中)不是在所有者的事件循环上执行的。 相同的组件可以在所有者的事件循环上运行,而无需修改, 或者,稍加修改,使component函数pickle成为可能,改为在工作进程中运行。