Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中,当使用多处理池时,主进程在某些情况下无法完成_Python_Multithreading_Queue_Pool - Fatal编程技术网

在python中,当使用多处理池时,主进程在某些情况下无法完成

在python中,当使用多处理池时,主进程在某些情况下无法完成,python,multithreading,queue,pool,Python,Multithreading,Queue,Pool,第一种情况,主流程无法完成 from multiprocessing import Pool, Queue queue = Queue() def handle(slogan): for i in xrange(100000): queue.put(slogan) print 'put done' def main(): pools = Pool(2) for i in xrange(4): pools.apply_a

第一种情况,主流程无法完成

from multiprocessing import Pool, Queue

queue = Queue()

def handle(slogan):

    for i in xrange(100000):
        queue.put(slogan)
    print 'put done'  

def main():

    pools = Pool(2)
    for i in xrange(4):
        pools.apply_async(handle, args=('test', ))   
    print 'waiting all done...'

    pools.close()
    pools.join()

    print 'all done...'


if __name__ == '__main__':

    main()
此代码的结果如下所示:

waiting all done...
put done
put done
put done
put done
我已经等了一个多小时了。我不明白。我认为多处理模块有一些bug或其他东西。所以我改变了这个密码。这次我没有使用多重处理队列,我只是用它来计算一些数字。代码如下:

from multiprocessing import Pool

def handle(slogan):

    tmp = 0
    for i in xrange(100000):
         tmp += i
    print 'put done'


def main():

    pools = Pool(2)
    for i in xrange(4):
        pools.apply_async(handle, args=('test', )) 
    print 'waiting all done...'

    pools.close()
    pools.join()

    print 'all done...'


if __name__ == '__main__':

    main()
对于代码,它已成功完成,结果如下:

waiting all done...
put done
put done
put done
put done
all done...

就因为我使用队列?我不知道为什么。谁能为我解释一下呢?

你没有捕捉到结果。您应该从
apply\u async()
捕获返回值,并对每个返回值调用
get()

另外,请尝试在
join()
get()
中指定一个较大的
timeout
值。在某些版本的Python中,这是解决bug所必需的


另请参见:

您没有捕获结果。您应该从
apply\u async()
捕获返回值,并对每个返回值调用
get()

另外,请尝试在
join()
get()
中指定一个较大的
timeout
值。在某些版本的Python中,这是解决bug所必需的

另见: