首页 > 解决方案 > 多处理——从多个运行时接收所有消息?

问题描述

我一直在尝试制作一个允许多个其他程序连接并向单个端口发送消息的程序。问题是,在某些时候,似乎在多处理模块的深处,os.read对两端的调用都会挂起。

演示receive.py

from multiprocessing.connection import Listener


while True:
    with Listener(("localhost", 10000), authkey=b"test") as listener:
        with listener.accept() as connection:
            print(connection.recv())

Demo send.py(发送1000次模仿多个程序,暴露问题):

from multiprocessing.connection import Client


for i in range(1000):
    successfully_sent = False
    while not successfully_sent:
        try:
            with Client(("localhost", 10000), authkey=b"test") as client:
                client.send(i)
        except (ConnectionRefusedError, ConnectionResetError):
            continue
        successfully_sent = True

任何有关如何强制进程继续而不丢失任何消息的提示将不胜感激。我考虑过在receive.py 中使用线程来强制超时,但我希望有一种更简洁的方法可以解决根本问题。另外,这是一个错误吗?

标签: pythonpython-3.xpython-multiprocessingpython-os

解决方案


推荐阅读