python multiprocess queue_Python multiprocessing.Queue vs multiprocessing.manager().Queue()

multiprocessing.Queue()和multiprocessing.Manager().Queue()的主要区别在于管理方式和使用场景。前者是不可 pickle 的,不能直接传递给池方法,且可能产生顺序问题;后者是共享队列的代理,能跨进程传递,但在多进程写入时可能导致接收顺序混乱。使用Manager().Queue()可避免某些等待和死锁问题,但在性能上可能稍逊一筹。
摘要由CSDN通过智能技术生成

虽然我对这个主题的理解有限,但从我所做的事情可以看出,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(),而且不那么麻烦。使用经理可能有一些缺点,但我不知道。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值