[英]Return on first result with Python multiprocessing
I'm attempting to generate a set of random values that fulfill certain requirements. 我正在尝试生成一组满足某些要求的随机值。
I'd like to do this in parallel to try and speed it up. 我想并行执行此操作以尝试加快速度。 That is, something like this: 也就是说,像这样:
pool = multiprocessing.Pool(8)
pool.map(gen_random_set, [set_size]*8)
However, I don't need multiple sets, just one. 但是,我不需要多套,只需一套。 How do I terminate the pool (presumably using pool.terminate()
) after the very first set is found? 找到第一个集后,如何终止池(大概使用pool.terminate()
)?
here is an example of terminating : 这是终止的示例:
import multiprocessing
from multiprocessing import Pool
def worker(i):
time.sleep(3)
print(i)
mypool = Pool()
a = mypool.map_async(worker,range(1000))
when you run this , your pool execution will stop: 运行this时,池执行将停止:
multiprocessing.pool.Pool.terminate(mypool)
the name 'mypool' should be same as the pool object you created by mypool=Pool() 名称“ mypool”应与您通过mypool = Pool()创建的池对象相同
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.