【发布时间】:2019-12-08 05:53:34
【问题描述】:
我正在尝试并行运行具有循环返回值的函数。
但在 for 循环的第二次迭代中,它似乎停留在 results = pool.map(algorithm_file.foo, population)
raise ValueError("Pool not running")
ValueError: Pool not running
示例代码:
from multiprocessing.dummy import Pool
import algorithm_file
population = [1, 3, 4]
pool = Pool(len(population))
total = list()
for _ in range(10):
results = pool.map(algorithm_file.foo, population)
pool.close()
pool.join()
total.append(sum(results))
print(total)
algorithm_file.py 内的内容
from random import randint
def foo(x):
return x * randint(0,5)
我尝试将pool = Pool(len(population))放在for循环中,但程序中途崩溃了,没有异常。
我发现一些解决方案使用全局列表()。但是有没有办法用返回值来维护函数呢?
Python 3.7.3
【问题讨论】:
-
如果将
pool.close()移到print(total)之后会发生什么? -
同样的异常。
ValueError: Pool not running但这次是pool.join()而不是 'results = pool.map(algorithm_file.foo, population)' 并且它发生在第一次迭代中
标签: python multiprocessing return python-3.7