Working with dicts in IPython.parallel

In [10]:
def scatter_dict(view, name, d):
    """partition a dictionary across the engines of a view"""
    ntargets = len(view)
    keys = d.keys() # list(d.keys()) in Python 3
    for i, target in enumerate(view.targets):
        subd = {}
        for key in keys[i::ntargets]:
            subd[key] = d[key]
        view.client[target][name] = subd
In [2]:
from IPython import parallel
In [3]:
rc = parallel.Client()
dv = rc[:]
dv
Out[3]:
<DirectView [0, 1, 2, 3]>
In [4]:
test_dict = {'43':"lion", '34':"tiger", '343':"duck"}
In [11]:
scatter_dict(dv, 'test', test_dict)
In [12]:
dv['test']
Out[12]:
[{'343': 'duck'}, {'43': 'lion'}, {'34': 'tiger'}, {}]
In [21]:
def dostuff():
    d2 = {}
    for k,v in test.items():
        print k, v
        d2[k[::-1]] = v[::-1]
    return d2
In [24]:
ar = dv.apply_async(dostuff)
ar.wait()
ar.display_outputs()
ar.get()
[stdout:0] 343 duck
[stdout:1] 43 lion
[stdout:2] 34 tiger

Out[24]:
[{'343': 'kcud'}, {'34': 'noil'}, {'43': 'regit'}, {}]
In [19]:
def gather_dict(view, name):
    """gather dictionaries from a DirectView"""
    merged = {}
    for d in view.pull(name):
        merged.update(d)
    return merged
In [20]:
gather_dict(dv, 'test')
Out[20]:
{'34': 'tiger', '343': 'duck', '43': 'lion'}
In []:
 
Back to top