0

所以我写了一些基本结构如下的代码:

from numpy import *
from dataloader import loadfile
from IPython.parallel import Client
from clustering import * 

data = loadfile(0)

N_CLASSES = 10
rowmax = nanmax(data.values, 0)
rowmin = nanmin(data.values, 0)

# Defines the size of block processed at a time
BLOCK_SIZE = 50000
classmean, classcov, classcovinv, classlogdet, classlogprob = init_stats(data, N_CLASSES, rowmax, rowmin)

client = Client()
ids    = client.ids
nodes  = len(ids)
view   = client.load_balanced_view()
dview  = client[:]

def get_ml_class(data, args): do sth
dview.scatter('datablock', data)
dview.execute('res1, res2 = get_ml_class(datablock, args)', block=False)

dview.execute 部分的输出是

<AsyncResult: execute>

这意味着它被执行,但是,当我试图通过

dview.pull(['res1','res2'], block=True)

表明:

NameError: name 'res1' is not defined

有人可以告诉我我的代码有什么问题吗?太感谢了!

4

1 回答 1

0

让我们做一个更简单的例子:

from IPython.parallel import Client

rc = Client()
dview = rc[:]
dview.scatter('a', Range(16))
dview.execute('res1,res2 = a[0], a[1]', block=False)
dview.pull(['res1'], block=True)

这可以按预期工作并给出结果:

[[0], [4], [8], [12]]

所以,我们至少做对了这一点。但是让我稍微修改一下代码:

from IPython.parallel import Client

rc = Client()
dview = rc[:]
dview.scatter('a', Range(16))
dview.execute('res1,res2 = a[0], b[1]', block=False)
dview.pull(['res1'], block=True)

现在我们有了NameError. 为什么?

因为该行有错误execute(它引用了b不存在的变量)。非阻塞execute并没有太多抱怨。在第一个(工作)案例中,状态为:

<AsyncResult: finished>

在第二种(非工作)情况下:

<AsyncResult: execute>

除此之外它非常安静,第二条消息并不一定意味着发生了错误。要查看真正的错误消息,请更改blockingTrue. 然后你会看到出了什么问题。

如果您想知道您的非阻塞执行是否有效,您必须AsyncResult捕获execute. 它有几个有趣的方法,但你最感兴趣的是ready方法successful

ar = dview.execute(...)
ar.ready()                # True if the process has finished
ar.successful()           # True if there were no exceptions raised

get此外,可以通过使用对象的方法来获取执行期间可能引发的异常AsyncResult。例如,我在交互式 shell 中给出的不好的例子:

>>> ar.get()

[0:execute]: 
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)<ipython-input-1-608f57d70b2f> in <module>()
----> 1 res1,res2=a[0]**2,b[1]**2
NameError: name 'b' is not defined

[1:execute]: 
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)<ipython-input-1-608f57d70b2f> in <module>()
----> 1 res1,res2=a[0]**2,b[1]**2
NameError: name 'b' is not defined

...

因此,总结一下:尝试找出您尝试远程运行的功能出了什么问题。现在它似乎提出了一些例外。该错误可能与args远程脚本似乎不可用有关。也许scatter缺少一个?

于 2014-06-25T18:46:57.977 回答