5

I am using the following test code:

from pathos.multiprocessing import ProcessingPool as Pool
import numpy

def foo(obj1, obj2):
   a = obj1**2
   b = numpy.asarray(range(1,5))
   return obj1, b

if __name__ == '__main__':
    p = Pool(5)
    res = p.map(foo, [1,2,3], [4,5,6])

It gives error:

File "C:\Python27\lib\site-packages\multiprocess\pool.py", line 567, in get
    raise self._value
NameError: global name 'numpy' is not defined

What am I doing wrong in the code?

Edit: Why was this question voted down twice?

I have numpy installed and my interpreter has been using it correctly until I try to do it for multiprocessing. I have been coding with same install for a while.

4

1 回答 1

3

似乎进程之间不共享导入。因此,您需要import numpy在所有流程中单独进行。

在您的情况下,这意味着import numpy在您的foo函数中添加。流程不是轻量级的,因此import不会减慢您的速度(至少不会显着)。

另一种选择是将模块传递给函数(不推荐,我不确定这是否可行):

if __name__ == '__main__':
    p = Pool(5)
    res = p.map(foo, numpy, [1,2,3], [4,5,6])

def foo(np, obj1, obj2):
   a = obj1**2
   b = np.asarray(range(1,5))
   return obj1, b
于 2016-08-04T20:39:15.230 回答