【发布时间】:2016-01-24 18:39:00
【问题描述】:
假设我们有一些使用 pool.apply_async() 生成的进程。当其中任何一个返回值时,如何停止所有其他进程? 另外,这是获得算法运行时间的正确方法吗? 这是示例代码:-
import timeit
import multiprocessing as mp
data = range(1,200000)
def func(search):
for val in data:
if val >= search:
# Doing something such that other processes stop ????
return val*val
if __name__ == "__main__":
cpu_count = mp.cpu_count()
pool = mp.Pool(processes = cpu_count)
output = []
start = timeit.default_timer()
results = []
while cpu_count >= 1:
results.append(pool.apply_async(func, (150000,)))
cpu_count = cpu_count - 1
output = [p.get() for p in results]
stop = timeit.default_timer()
print output
pool.close()
pool.join()
print "Running Time : " + str(stop - start) + " seconds"
【问题讨论】:
标签: python