多处理numpy的未定义错误 [英] multiprocessing numpy not defined error

查看:328
本文介绍了多处理numpy的未定义错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下测试代码:

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])

出现错误:

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?

为什么这个问题被否决两次?

Why was this question voted down twice?

我已经安装了numpy,并且我的解释器一直在正确使用它,直到尝试将其用于多处理为止.我已经用相同的安装编码了一段时间了.

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.

推荐答案

似乎导入在进程之间不共享.因此,您需要分别在所有进程中import numpy.

It seems like imports are not shared between processes. Therefore you need to import numpy in all your processes seperatly.

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

In your case this means adding the import numpy in your foo function. Processes are not light-weight so the import won't slow you down (at least not significantly).

另一种选择是将模块传递给功能(不推荐,我不确定是否可以使用):

The other alternative would be to pass the module to the functions (not recommended and I'm not sure if that will work):

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

这篇关于多处理numpy的未定义错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆