使并行代码在python 2.7和3.6中工作
我在python 3.6中有一些代码如下:使并行代码在python 2.7和3.6中工作,python,python-2.7,python-3.x,multiprocessing,starmap,Python,Python 2.7,Python 3.x,Multiprocessing,Starmap,我在python 3.6中有一些代码如下: from multiprocessing import Pool with Pool(processes=4) as p: p.starmap(parallel_function, list(dict_variables.items())) [('aa', ['ab', 'ab', 'ad']), ('aa1', ['a1b', 'a1b', 'a2d'])] 这里的dict_变量如下所示: from multiprocessing imp
from multiprocessing import Pool
with Pool(processes=4) as p:
p.starmap(parallel_function, list(dict_variables.items()))
[('aa', ['ab', 'ab', 'ad']), ('aa1', ['a1b', 'a1b', 'a2d'])]
这里的dict_变量如下所示:
from multiprocessing import Pool
with Pool(processes=4) as p:
p.starmap(parallel_function, list(dict_variables.items()))
[('aa', ['ab', 'ab', 'ad']), ('aa1', ['a1b', 'a1b', 'a2d'])]
此代码仅适用于python 3.6。如何使其在2.7中工作?starmap
was。在Python2中,自己使用并解压参数:
在Python3中:
import multiprocessing as mp
def starmap_func(x, y):
return x**y
with mp.Pool(processes=4) as p:
print(p.starmap(starmap_func, [(1,2), (3,4), (5,6)]))
# [1, 81, 15625]
在蟒蛇2或蟒蛇3中:
import multiprocessing as mp
def map_func(arg):
x, y = arg
return x**y
p = mp.Pool(processes=4)
print(p.map(map_func, [(1,2), (3,4), (5,6)]))
# [1, 81, 15625]
p.close()
Python的旧版本不能处理这种代码。您只能将旧代码转换为新版本(大多数情况下)