虽然我对这个主题的理解有限,但从我所做的事情可以看出,multiprocessing.Queue()和multiprocessing.Manager().Queue()之间有一个主要的区别:multiprocessing.Queue()是一个对象,而multiprocessing.Manager().Queue()是指向由multiprocessing.Manager()对象管理的共享队列的地址(代理)。
因此,您不能将普通的multiprocessing.Queue()对象传递给池方法,因为它不能被pickle。
此外,python doc告诉我们在使用multiprocessing.Queue()时要特别注意,因为它可能会产生不希望的效果Note When an object is put on a queue, the object is pickled and a background thread later flushes the pickled data to an underlying pipe. This has some consequences which are a little surprising, but should not cause any practical difficulties – if they really bother you then you can instead use a queue created with a manager.
After putting an object on an empty queue there may be an infinitesimal delay before the queue’s empty() method returns False and get_nowait() can return without raising Queue.Empty.
If multiple processes are enqueuing objects, it is possible for the objects to be received at the other end out-of-order. However, objects enqueued by the same process will always be in the expected order with respect to each other.
Warning As mentioned above, if a child process has put items on a queue (and it has not used JoinableQueue.cancel_join_thread), then that process will not terminate until all buffered items have been flushed to the pipe.
This means that if you try joining that process you may get a deadlock unless you are sure that all items which have been put on the queue have been consumed. Similarly, if the child process is non-daemonic then the parent process may hang on exit when it tries to join all its non-daemonic children.
Note that a queue created using a manager does not have this issue.
通过将队列设置为全局变量并在初始化时为所有进程设置该变量,可以解决对池使用multiprocessing.Queue()的问题:queue = multiprocessing.Queue()
def initialize_shared(q):
global queue
queue=q
pool= Pool(nb_process,initializer=initialize_shared, initargs(queue,))
将创建具有正确共享队列的池进程,但我们可以争辩说,多处理.Queue()对象不是为此用途创建的。
另一方面,可以在池子进程之间共享manager.Queue(),方法是将其作为函数的普通参数传递。
在我看来,在任何情况下都可以使用multiprocessing.Manager().Queue(),而且不那么麻烦。使用经理可能有一些缺点,但我不知道。