将复杂字典放入返回queu时,多处理进程不联接

2024-07-08 11:27:37 发布

您现在位置:Python中文网/ 问答频道 /正文

给定一个相当标准的读/写多线程进程,其中有一个读队列和一个写队列:

打印了8次worker done,但join()语句从未被传递。但是如果我用'queue替换queue_out.put(r)_输出(1) 它起作用了。在

可能是我的大脑在融化。我应该复制我的字典并把它放在归还队列中吗?我是不是犯了个愚蠢的错误?在

流程功能

def reader(queue_in, queue_out, funktion):
    # Read from the queue
    while True:
        r = queue_in.get()
        if r == 'DONE':
            return
        funktion(r) # funktion adds additional keys to the dictionary
        queue_out.put(r) # <---- replacing r by 1 does let me join()
    print "worker done" # <----- this happens

填充输入队列

^{pr2}$

其余

WORKERS = 8

# init Queues
queue_in = Queue()
queue_out = Queue()

# Start processes, with input and output quests
readers = []
for _ in range(0, WORKERS):
    p = Process(target=reader, args=(queue_in, queue_out, funktion))
    p.daemon = True
    p.start()
    readers.append(p)

writer(generator, queue_in)

for p in readers:
    p.join()

print "joined"  # <---- this never happens

queue_in.close()

while not queue_out.empty():
    print queue_out.get()
queue_out.close()

Tags: theintrue队列queueputoutreader
1条回答
网友
1楼 · 发布于 2024-07-08 11:27:37

我想我是从两个来源拼凑起来的,因为我总是有同样的问题。我认为重要的是这是在Windows中。在

来自the documentation的注释

Since Windows lacks os.fork() it has a few extra restrictions:

然后阅读答案here,即{}是forked processed的。在

我总是设法以与您类似的方式运行multiprocessing,而不使用join(),并且没有看到任何错误—我非常高兴有一个反例来解释为什么需要它。实际上,删除它已经纠正了您的问题。在

并且this article更深入地讨论了操作系统之间的多处理与子进程的区别。我确实认为,join()的问题应该在文档中更加明确。在

相关问题 更多 >

    热门问题