Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使并行代码在python 2.7和3.6中工作_Python_Python 2.7_Python 3.x_Multiprocessing_Starmap - Fatal编程技术网

使并行代码在python 2.7和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

我在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 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的旧版本不能处理这种代码。您只能将旧代码转换为新版本(大多数情况下)