为什么join()会破坏更大的数据集?

时间:2014-12-01 19:45:07

标签: python multiprocessing

我处理一些我分派到队列中的数据,这些数据由工作人员使用,后者将其结果放在另一个队列中,最后解析该队列以获得最终结果。以下示例将self.size乘以队列中的数字1,工作人员将值10放回:

import multiprocessing
import Queue

class Dispatch():
    def __init__(self):
        self.q_in = multiprocessing.Queue()
        self.q_out = multiprocessing.Queue()
        self.maxproc = 4
        self.size = 1000

    def putdata(self):
        # put data to the queue
        for d in range(self.size):
            self.q_in.put(1)
        # put sentinels at the end of the FIFO queue
        for i in range(self.maxproc):
            self.q_in.put("STOP")

    def processing(self):
        procs = []
        for i in range(self.maxproc):
            p = multiprocessing.Process(target=self.multiply)
            procs.append(p)
            p.start()
        return procs

    def multiply(self):
        while True:
            item = self.q_in.get()
            if item == 'STOP':
                print("{0} exiting".format(multiprocessing.current_process()))
                return
            self.q_out.put(item * 10)

    def getdata(self):
        total = 0
        while True:
            try:
                item = self.q_out.get(block=False)
                total += item
            except Queue.Empty:
                print("done getdata")
                break
        print("data out: {0}".format(total))

if __name__=="__main__":
    dis = Dispatch()
    procs = dis.processing()
    dis.putdata()
    for p in procs:
        p.join()
    dis.getdata()
    print("done __main__")

self.size变大时,此代码会中断。例如,它适用于self.size = 1000

<Process(Process-1, started)> exiting
<Process(Process-4, started)> exiting
<Process(Process-2, started)> exiting
<Process(Process-3, started)> exiting
done getdata
data out: 10000
done __main__

然而,对于self.size = 10000,它会输出

<Process(Process-2, started)> exiting
<Process(Process-4, started)> exiting
<Process(Process-1, started)> exiting
<Process(Process-3, started)> exiting

并挂起。看起来它围绕着join()来电(因为流程return已编辑但未达到getdata()

为什么会这样?队列的大小是否有限制? (我在运行期间监视了内存和CPU,但它们没问题)或者是其他的东西?

0 个答案:

没有答案