简体   繁体   English

使用队列在进程之间进行通信

[英]Using queue to communicate between processes

I have a single process A a the first Pool, and several processes B1..Bk in the second pool and I would like to put items into the queue in A and consume items in B1..Bk. 我在第一个池中有一个进程A,在第二个池中有多个进程B1..Bk,我想将项目放入A的队列中并使用B1..Bk中的项目。

My first attempt was to just create multiprocessing.Queue and pass it to all those processes. 我的第一次尝试是只创建multiprocessing.Queue并将其传递给所有这些进程。 However this gave me the error 但这给了我错误

RuntimeError: Queue objects should only be shared between processes through inheritance

I found advice that suggests to use multiprocessing.Manager().Queue() instead. 我发现建议改用multiprocessing.Manager()。Queue()。 But when I do this and try to read from the queue in Bi, I get the error 但是当我这样做并尝试从Bi中的队列中读取时,我得到了错误

TypeError: 'AutoProxy[Queue]' object is not iterable

So what is the correct way to do this? 那么正确的方法是什么呢?

OK, this was just sillyness from my side, I misunderstood what a Queue is! OK,这只是我的愚蠢,我误解了队列是什么! Most importantly a queue is not iterable so one cannot do "for el in somequeue". 最重要的是,队列是不可迭代的,因此不能“为某些情况而行”。 (My mistake was to think that the queue proxy is not iterable because it is the proxy. However the proxy works fine in place of the actual queue if put/get are used) Also, a (FIFO) queue cannot be closed and does not have a natural "end" which I found annoying because it means one has to send around special "end of queue" entries but not too many of them to not unintentionally block the queue. (我的错误是认为队列代理是不可迭代的,因为它是代理。但是,如果使用put / get,则该代理可以代替实际队列正常工作)而且,(FIFO)队列无法关闭并且不能有一个自然的“结束”,我发现这很烦人,因为这意味着必须发送特殊的“队列结束”条目,但不要过多地发送它们以防止无意间阻塞队列。

So the bottom line is: to share the queue I create a multiprocessing.Manager().Queue() and pass that around, then I use put/get to write/read the queue in different processes and I send some special entry to the reader to indicate end of job. 因此,最重要的是:要共享队列,我创建了一个multiprocessing.Manager()。Queue()并将其传递,然后在不同的进程中使用put / get写入/读取队列,并将一些特殊条目发送给读者指出工作即将结束。

That a queue cannot get closed and the consumer get an "end of queue" condition is really annoying though, especially when there is an error situation: if a queue is consumed by k consumers, then the writer has to know k and send k end of job indicators and the k consumers all have to be well behaved to retrieve those and shut down. 但是,无法关闭队列而使使用者获得“队列结束”条件确实很烦人,尤其是在出现错误情况时:如果k个使用者使用了队列,那么编写者必须知道k并发送k结束工作指标和k个消费者都必须表现良好,以取回这些指标并关闭。 If there is any error, all this cannot be guaranteed any more and eg a consumer may lock or timeout waiting for the end of job indicator that will never arrive. 如果存在任何错误,将无法再保证所有这些错误,例如,消费者可能会锁定或超时,以等待永远不会到达的作业结束指示符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用文件描述符在进程之间进行通信 - Using file descriptors to communicate between processes 如何在管道,队列,值,数组和管理器之间进行选择以在进程之间进行通信? - How do I choose between Pipe, Queue, Value, Array and Manager to communicate between processes? 在进程之间共享队列 - Share queue between processes 在不使用多处理模块的情况下在Python进程之间排队 - Queue between Python processes without using the multiprocessing module 在进程之间通信资源锁的最佳方式 - Best way to communicate resource lock between processes 在流程之间进行多处理通信的最佳方式。 - Best way to communicate in multiprocessing between processes. 在基于三重奏的 Python 应用程序中生成进程并在进程之间进行通信 - Spawn processes and communicate between processes in a trio based Python application 获取“仅应通过继承在进程之间共享队列对象”,但我没有使用队列 - Getting “Queue objects should only be shared between processes through inheritance” but I'm not using a Queue 在池进程之间共享队列对象 - Share a queue object between pool processes 2个进程之间的raspberry pi 3多处理队列同步 - raspberry pi 3 multiprocessing queue syncronization between 2 processes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM