You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python多进程中父子进程队列共享异常问题排查与解决

问题原因与解决方案

这问题我之前踩过坑!核心原因是你用的multiprocessing.Queue在Pool场景下没法实现真正的跨进程共享——当Pool启动子进程时,会复制父进程的内存空间,每个子进程拿到的都是Queue的独立副本,不是同一个队列!

你看到子进程里打印的队列长度正确,是因为子进程往自己的那个副本队列里塞了数据,但父进程手里的原始队列根本没收到这些内容,所以父进程打印的长度是0。

解决方案1:用multiprocessing.Manager().Queue()实现共享队列

Manager会创建一个专门的管理进程来维护共享对象,所有子进程都会和这个共享队列交互,而不是操作副本。修正后的代码如下:

import multiprocessing

def task(n, que):
    que.put(n)
    print("Child: queue len is %d" % que.qsize())

if __name__ == '__main__':
    # 使用Manager创建共享队列
    with multiprocessing.Manager() as manager:
        que = manager.Queue()
        pool = multiprocessing.Pool()
        
        # 用starmap传递多参数(因为map只能传一个参数)
        pool.starmap(task, [(n, que) for n in range(10)])
        
        # 必须关闭池并等待所有子进程完成,确保所有put操作都执行完毕
        pool.close()
        pool.join()
        
        print("Parent: queue len is %d" % que.qsize())
        # 读取队列里的所有结果
        while not que.empty():
            print(f"Got result: {que.get()}")

运行这段代码后,父进程就能正确读到队列长度为10,并且能取出所有子进程放入的数据。

解决方案2:直接用Pool的返回值收集结果

如果你的需求只是在所有任务完成后收集结果,不需要实时通过Queue传递数据,那直接用pool.map的返回值会更简单——pool.map会自动把所有子进程的返回值收集到父进程的列表里:

import multiprocessing

def task(n):
    print(f"Child: processing {n}")
    return n

if __name__ == '__main__':
    pool = multiprocessing.Pool()
    # map会自动收集所有子进程的返回值
    results = pool.map(task, range(10))
    pool.close()
    pool.join()
    
    print(f"Parent: collected {len(results)} results")
    print(f"Results: {results}")

这种方法避免了Queue的共享问题,代码更简洁,适合大多数批量任务收集结果的场景。

内容的提问来源于stack exchange,提问作者chenchang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:00:24