Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.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 如何将(x,y)对列表映射到Pool.map中的函数f(x,y)?_Python_Python 2.7_Python 3.x_Parallel Processing_Arguments - Fatal编程技术网

Python 如何将(x,y)对列表映射到Pool.map中的函数f(x,y)?

Python 如何将(x,y)对列表映射到Pool.map中的函数f(x,y)?,python,python-2.7,python-3.x,parallel-processing,arguments,Python,Python 2.7,Python 3.x,Parallel Processing,Arguments,假设我想要在x-y平面上绘制密度,密度定义为: def density(x,y): return x**2 +y**2 如果我有很多点要计算,所以我想并行计算。我找到了文档并尝试执行以下操作: pointsList = [(1,1), (2,2), (3,3)] from multiprocessing import Pool if __name__ == '__main__': with Pool() as p: print(p.map(density,poi

假设我想要在x-y平面上绘制密度,密度定义为:

def density(x,y):
    return x**2 +y**2
如果我有很多点要计算,所以我想并行计算。我找到了文档并尝试执行以下操作:

pointsList = [(1,1), (2,2), (3,3)]
from multiprocessing import Pool
if __name__ == '__main__':
    with Pool() as p:
        print(p.map(density,pointsList ))
错误发生了,我似乎无法将参数传递给函数,如何做到这一点


编辑: 错误是:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-647-1e2a1f0007fb> in <module>()
      5 from multiprocessing import Pool
      6 if __name__ == '__main__':
----> 7     with Pool() as p:
      8         print(p.map(density,pointsList ))

AttributeError: __exit__
---------------------------------------------------------------------------
AttributeError回溯(最近一次呼叫上次)
在()
5来自多处理导入池
6如果uuuu name uuuuuu=='\uuuuuuuu main\uuuuuuuuu':
---->7,池()为p:
8打印(p.map(密度、点列表))
AttributeError:\uuu退出__

编辑2:
如果我不能在
python2.7
中实现这个简单的并行,那么我怎么能在
python3.5
中实现呢?

在Python 3.3中添加了在上下文管理器中使用
Pool
。由于标记了Python2.7,因此不能将
语法一起使用

:

版本3.3中的新功能:池对象现在支持上下文管理 协议–请参阅上下文管理器类型<代码>\uuuu输入\uuuuu()返回池 对象,并调用
终止()

下面是您想要的python 3.3+工作示例:

def density(args):
    x, y = args
    return x**2 +y**2

pointsList = [(1,1), (2,2), (3,3)]
from multiprocessing import Pool
if __name__ == '__main__':
    with Pool() as p:
        print(p.map(density,pointsList ))
由于您也在使用Python2.7,您只需不使用上下文管理器而调用
p.terminate()

def density(args):
    x, y = args
    return x**2 +y**2

pointsList = [(1,1), (2,2), (3,3)]
from multiprocessing import Pool
if __name__ == '__main__':
    p = Pool()
    print(p.map(density,pointsList ))
    p.terminate()

需要更改密度函数以解压缩元组参数

def density(z):
    (x,y) = z
    return x**2 +y**2
尽量不要将
一起使用,并在使用完后自己关闭池。
这种方式应该兼容Python2和Python3

from multiprocessing import Pool  
pointsList = [(1,1), (2,2), (3,3)]
p = Pool()
print(p.map( density,pointsList ))
p.close()
或者使用
contextlib
模块

from multiprocessing import Pool 
import contextlib
pointsList = [(1,1), (2,2), (3,3)]
with contextlib.closing(Pool()) as p:
    print(p.map( density,pointsList ))

“错误发生了”。说有一个错误,而不说错误是什么,这没有多大帮助。我从来没有说过你不能在python 2.7中这样做。。。我只是说你不能用
语法。你使用了错误的文档,这是Python2:@abccd无论是2.7还是3.3,我都希望它能工作,你能在回答中给出一个有效的例子吗,尽管版本不同。在这里,我为Python3.5添加了一个工作示例,那么如何在3.5中使用它呢?只要python版本>=3.3,您就可以做您在文章中正在做的事情。这给了我一个错误:
TypeError:density()缺少一个必需的位置参数:“y”
您尝试过这个吗?它给了我一个错误
TypeError:density()在
3
中缺少1个必需的位置参数:“y”
,抱歉,没有注意到,请尝试编辑的一个,它对我有效