【发布时间】:2020-10-31 06:21:45
【问题描述】:
我对 Python 中的多处理有疑问。我需要创建异步进程,该进程运行时间未定义,进程数也未定义。新请求一到达,就必须使用请求中的规范创建一个新流程。我们使用 ZeroMQ 进行消息传递。还有一个 Process 是从一开始就开始,只有在整个脚本终止时才结束。
现在我正在寻找一种解决方案,如何等待所有进程,同时能够添加其他进程。
asyncio.gather()
这是我的第一个想法,但它在被调用之前需要进程列表。
class Object:
def __init__(self, var):
self.var = var
async def run(self):
*do async things*
class object_controller:
def __init__(self):
self.ctx = zmq.Context()
self.socket = self.ctx.socket(zmq.PULL)
self.socket.connect("tcp://127.0.0.1:5558")
self.static_process = AStaticProcess()
self.sp = aiomultiprocess.Process(target=self.static_process.run)
self.sp.start()
#here I need a good way to await this process
def process(self, var):
object = Object(var)
process = aiomultiprocess.Process(target=object.run)
process.start()
def listener(self)
while True:
msg = self.socket.recv_pyobj()
# here I need to find a way how I can start and await this process while beeing able to
# receive additional request, which result in additional processes which need to be awaited
这是一些希望能解释我的问题的代码。我需要一种等待进程的收集器。
初始化之后,对象和控制器之间没有交互,只有zeroMQ(静态进程和变量进程之间)。也没有回报。
【问题讨论】:
标签: python python-asyncio zeromq