是否可以使用multiprocessing.Event为进程池实现同步屏障? [英] Is it possible to use multiprocessing.Event to implement a synchronization barrier for pool of processes?
本文介绍了是否可以使用multiprocessing.Event为进程池实现同步屏障?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以使用multiprocessing.Event为进程池实现同步屏障?我正在使用Python 2.7.但是,看起来事件未在进程之间共享. 我的代码有问题吗?
Is it possible to use multiprocessing.Event to implement a synchronization barrier for pool of processes? I am using Python 2.7. But it looks like the Event is not shared between processes. Is something wrong with my code?
def test_func(event):
event.wait()
return datetime.datetime.now()
def log_result(result):
result_list.append(result)
if __name__ == '__main__':
pool_size=10
pool = multiprocessing.Pool(processes=pool_size)
event = multiprocessing.Manager().Event()
for _ in xrange(pool_size):
pool.apply_async(test_func, args=(event,), callback=log_result)
pool.close()
pool.join()
time.sleep(5)
event.set()
推荐答案
您有死锁:pool.join()等待启动的进程,所有进程都等待event.wait(). Sedd池加入:
You have dead-lock: pool.join() waits for started processes and all processes wait on event.wait(). Sedd pool join:
join()
Wait for the worker processes to exit
基本上,您必须将"pool.join()"移到"event.set()"之后,并且一切都应该没问题.
Basically you have to move "pool.join()" after "event.set()" and all should be OK.
这篇关于是否可以使用multiprocessing.Event为进程池实现同步屏障?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文