IPython并行引用与地图VS适用

时间:2015-07-10 14:36:27

标签: reference parallel-processing ipython

在使用map和dict之类的参数时,我无法获得并行引用。

from IPython import parallel
from IPython.parallel import Client
from IPython.parallel import Reference

rc = Client()
dview = rc.direct_view()
dview.block = True

dview['l'] = [0, 1]
kws = [{'l': Reference('l')}]
def second(kws):
    l = kws['l']
    return l[1]

dview.apply(second, kws[0])

返回:

[1, 1, 1, 1, 1, 1, 1, 1]

可是:

dview.map(second, kws)

返回:

TypeError: 'Reference' object does not support indexing

1 个答案:

答案 0 :(得分:1)

问题是反序列化不能很好地处理嵌套结构。你可以这样看待这个:

from IPython.core.display import display
from IPython.kernel.zmq.serialize import serialize_object, unserialize_object
from IPython.parallel import Reference
l = 5
ref = Reference('l')
kws = [dict(l=ref)]
display(unserialize_object(serialize_object(kws), g=globals()))
# outputs ([{'l': <Reference: 'l'>}], [])
display(unserialize_object(serialize_object(kws[0]), g=globals()))
# outputs ({'l': 5}, [])

我不是一个IPython开发人员,但在查看代码时我找到了一种非常简单的方法来解决它。在ipykernel/pickleutil.py(或IPython/utils/pickleutil.py)中,有一个字典可以将类型映射到反序列化方法:

uncan_map = {
    CannedObject : lambda obj, g: obj.get_object(g),
}

只需添加dict即可解决问题:

uncan_map = {
    CannedObject : lambda obj, g: obj.get_object(g),
    dict : uncan_dict,
}

我将针对引用此问题的ipykernel项目发出拉取请求。