gpt4 book ai didi

python multiprocessing - 进程挂起加入大队列

转载 作者:IT老高 更新时间:2023-10-28 20:26:16 24 4
gpt4 key购买 nike

我正在运行 python 2.7.3,我注意到以下奇怪的行为。考虑这个最小的例子:

from multiprocessing import Process, Queue

def foo(qin, qout):
while True:
bar = qin.get()
if bar is None:
break
qout.put({'bar': bar})

if __name__ == '__main__':
import sys

qin = Queue()
qout = Queue()
worker = Process(target=foo,args=(qin,qout))
worker.start()

for i in range(100000):
print i
sys.stdout.flush()
qin.put(i**2)

qin.put(None)
worker.join()

当我循环超过 10,000 个或更多时,我的脚本会卡在 worker.join() 上。当循环仅达到 1,000 时,它工作正常。

有什么想法吗?

最佳答案

子进程中的 qout 队列已满。您从 foo() 放入的数据不适合操作系统内部使用的管道缓冲区,因此子进程会阻止尝试容纳更多数据。但是父进程并没有读取这些数据:它也只是被阻塞了,等待子进程完成。这是典型的死锁。

关于python multiprocessing - 进程挂起加入大队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21641887/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com