19

I'm working on a fairly large project in Python that requires one of the compute-intensive background tasks to be offloaded to another core, so that the main service isn't slowed down. I've come across some apparently strange behaviour when using multiprocessing.Queue to communicate results from the worker process. Using the same queue for both a threading.Thread and a multiprocessing.Process for comparison purposes, the thread works just fine but the process fails to join after putting a large item in the queue. Observe:

import threading
import multiprocessing

class WorkerThread(threading.Thread):
    def __init__(self, queue, size):
        threading.Thread.__init__(self)
        self.queue = queue
        self.size = size

    def run(self):
        self.queue.put(range(size))


class WorkerProcess(multiprocessing.Process):
    def __init__(self, queue, size):
        multiprocessing.Process.__init__(self)
        self.queue = queue
        self.size = size

    def run(self):
        self.queue.put(range(size))


if __name__ == "__main__":
    size = 100000
    queue = multiprocessing.Queue()

    worker_t = WorkerThread(queue, size)
    worker_p = WorkerProcess(queue, size)

    worker_t.start()
    worker_t.join()
    print 'thread results length:', len(queue.get())

    worker_p.start()
    worker_p.join()
    print 'process results length:', len(queue.get())

I've seen that this works fine for size = 10000, but hangs at worker_p.join() for size = 100000. Is there some inherent size limit to what multiprocessing.Process instances can put in a multiprocessing.Queue? Or am I making some obvious, fundamental mistake here?

For reference, I am using Python 2.6.5 on Ubuntu 10.04.

4

3 回答 3

20

Seems the underlying pipe is full, so the feeder thread blocks on the write to the pipe (actually when trying to acquire the lock protecting the pipe from concurrent access).

Check this issue http://bugs.python.org/issue8237

于 2012-04-05T12:58:34.917 回答
2

python多处理的答案:某些函数在完成时不返回(队列材料太大)在并行执行任意一组函数时实现了您可能所说的“加入前出队”的意思,其返回值排队。

因此,这允许将任何大小的东西放入队列中,这样您找到的限制就不会妨碍您。

于 2012-08-08T18:24:38.943 回答
2

默认情况下,队列的 maxsize 是无限的,但您已经覆盖了它。在您的情况下,worker_p 正在将项目放入队列中,应该在调用加入之前释放队列。详情请参阅以下链接。 https://docs.python.org/2/library/multiprocessing.html#programming-guidelines

于 2015-06-27T18:47:59.150 回答