【发布时间】:2014-05-09 03:53:15
【问题描述】:
我有一个如下形式的算法:
V = {}
for i in range(N):
V[i] = {}
for j in range(10):
if should_skip(i,j):
continue
V[i][j] = do_something(V)
我已成功并行化如下:
V = {}
for i in range(N):
V[i] = {}
p = Pool(4) # On each iteration, start a new pool
results = []
for j in range(10):
if should_skip(i,j):
continue
results.append(p.apply_async(do_something, (V,)))
p.close()
p.join() # Then close and join the pool after forking the jobs
for result in results:
r = result.get() # do_something modified to return 'j' as its second arg
V[i][r[1]] = r[0]
我想知道,这是正确的方法吗?保持打开和关闭池不是很昂贵吗?有没有更好的方法来避免每次迭代都创建一个新池?
【问题讨论】:
标签: python parallel-processing multiprocessing threadpool pool