使用装饰器时出现ipyparallel错误

joh*_*ase 6 python ipython ipython-parallel jupyter-notebook

我试图使用ipyparallel系统运行一个工作,lru_cache并遇到问题.

从终端:

ipcluster start -n 2
Run Code Online (Sandbox Code Playgroud)

在ipython笔记本中:

from ipyparallel import Client
clients = Client()

def world():
    return hello() + " World"

def hello():
    return "Hello"

world()
'Hello World'
Run Code Online (Sandbox Code Playgroud)

使用ipyparallel运行它需要:

clients[:].push(dict(hello=hello))
Run Code Online (Sandbox Code Playgroud)

如果没有上一行,则以下操作失败,这不是意料之外的,但如果运行则可以正常工作:

clients[:].apply_sync(world)
['Hello World', 'Hello World']
Run Code Online (Sandbox Code Playgroud)

然而,这一切都按预期工作,lru_cache并行步骤会产生错误

from ipyparallel import Client
from functools import lru_cache

clients = Client()

def world():
    return hello() + " World"

@lru_cache(maxsize=2048)
def hello():
    return "Hello"

clients[:].push(dict(hello=hello))
clients[:].apply_sync(world)
Run Code Online (Sandbox Code Playgroud)

此操作失败,并显示以下错误:

  [0:apply]: 
---------------------------------------------------------------------------NameError                                 
Traceback (most recent call last)<string> in <module>()
<ipython-input-17-9ac5ef032485> in world()
NameError: name 'hello' is not defined

[1:apply]: 
---------------------------------------------------------------------------NameError                                 
Traceback (most recent call last)<string> in <module>()
<ipython-input-17-9ac5ef032485> in world()
NameError: name 'hello' is not defined
Run Code Online (Sandbox Code Playgroud)

我意识到这可能是一个lru_cache使用闭包的命名空间问题,但我希望有一个解决方法,以便我能够使用它.或者如果有人可以告诉我,这根本不可能也是有用的,谢谢.

我正在使用:
ipykernel(4.1.1)
ipyparallel(4.1.0)
ipython(4.0.1)
Python(3.5.1)