简体   繁体   English

IPython.parallel名称空间

[英]IPython.parallel namespaces

I want to parallelize a function using IPython.parallel, and when I define it in the IPython shell it works flawlessly: 我想使用IPython.parallel并行化一个函数,当我在IPython shell中定义它时,它可以完美地工作:

Type:       function
Base Class: <type 'function'>
String Form:<function gradient at 0x3ae0398>
Namespace:  Interactive
File:       /root/<ipython-input-30-cf7eabdfef84>
Definition: gradient(w) 
Source:
def gradient(w):
    s = (1.0 + exp(y * (X * w)))**-1
    return C*X.T*((1 - s) * y)

rc = Client() 
rc[:].apply_sync(gradient, w)
...

However, when I define it in a module and use import: 但是,当我在模块中定义它并使用import时:

Type:       function
Base Class: <type 'function'>
String Form:<function gradient at 0x3933d70>
Namespace:  Interactive
File:       /root/mv.py
Definition: mv.gradient(w)
Source:
def gradient(w):
    s = (1.0 + exp(y * (X * w)))**-1
    return C*X.T*((1 - s) * y)

import mv 
rc = Client()
rc[:].apply_sync(mv.gradient, w)

CompositeError: one or more exceptions from call to method: gradient
[0:apply]: NameError: global name 'y' is not defined
[1:apply]: NameError: global name 'y' is not define

Furthermore, it works fine one my local system running Python 2.7.2/IPython 0.12, while it crashes on Python 2.7.2+/IPython 0.12 using the newest Starcluster Ubuntu AMI. 此外,它运行Python 2.7.2 / IPython 0.12的本地系统工作正常,而使用最新的Starcluster Ubuntu AMI在Python 2.7.2 + / IPython 0.12上崩溃。

What is going on here? 这里发生了什么?

UPDATE: I installed the IPython 0.13.dev version from github and now it works. 更新:我从github安装了IPython 0.13.dev版本,现在它可以工作了。

The difference is module globals. 不同之处在于模块全局变量。 When a function is defined in a module, the global namespace is that of the module (ie mv.y ). 在模块中定义函数时,全局名称空间是模块的名称空间(即mv.y )。 When that module is __main__ , eg an interactively defined function, then the global namespace is your user_ns on the Engine, and is affected by execute("y=5") . 当该模块是__main__ ,例如交互式定义的函数,则全局命名空间是Engine上的user_ns,并受execute("y=5")

IPython provides a decorator, if you want to define functions in modules that should behave as if they are interactively defined (have access to the user namespace as globals): IPython提供了一个装饰器,如果你想在模块中定义函数,这些函数应该像交互式定义一样(可以作为全局变量访问用户命名空间):

# mymod

from IPython.parallel.util import interactive

@interactive
def by_a(b):
    """multiply a by b"
    return a*b

And interactively, you can do: 交互式地,您可以:

from mymod import by_a

e0 = rc[0]
e0.execute("a=5")
print e0.apply_sync(by_a, 10) # 50
e0.execute("a=10")
print e0.apply_sync(by_a, 10) # 100

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 IPython.parallel不使用多核? - IPython.parallel not using multicore? 如何在numpy / ipython.parallel中进行分布式矩阵乘法? - How to do a distributed matrix multiplication in numpy / ipython.parallel? 使用Sympy的lambdify和IPython.parallel时出错 - Errors using Sympy's `lambdify` with `IPython.parallel` 使用 sync_imports() 在 IPython.parallel 引擎上导入自定义模块 - Import custom modules on IPython.parallel engines with sync_imports() 如何将IPython.parallel用于具有多个输入的函数? - How to use IPython.parallel for functions with multiple inputs? 如何使用IPython.parallel map()与生成器作为函数的输入 - How to use IPython.parallel map() with generators as input to function 如何在IPython.parallel中使用交互式定义的类? - How to work with interactively-defined classes in IPython.parallel? IPython.parallel在load_balanced_mode()中的阻塞和非阻塞模式之间有什么区别? - What is the difference between blocking and non-blocking mode in IPython.parallel in load_balanced_mode()? IPython.parallel ValueError:无法从内存缓冲区创建OBJECT数组 - IPython.parallel ValueError: cannot create an OBJECT array from memory buffer IPython.parallel-我可以将自己的日志写入引擎日志吗? - IPython.parallel - can I write my own log into the engine logs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM