Friday, 3 October 2014

test ipython.parallel

from IPython import parallel
c = parallel.Client(packer='pickle')
c.block = True
print(c.ids)

dview = c.direct_view()
dview.block = True
print(dview)

dview.execute('import numpy as np')

###using execute
dview.execute('a=np.random.randint(0,5,size=(1,2))')
print("a:\n{}".format(dview.gather('a')))

###using run
f=open('iptest.py','w')
f.write('import numpy as np\nb=np.zeros(shape=(1,4),dtype=np.int32)')
f.close()
dview.run('iptest.py')
print("b:\n{}".format(dview.gather('b')))
print type(dview.gather('b')[0,0])

###using dictionary
dview['c']=np.ones(shape=(2,3),dtype=np.int8)
print("a:\n{}".format(dview.gather('c')))
print type(dview.gather('c')[0,0])

---------------output------------------
[0, 1, 2]
<DirectView all>
a:
[[4 0]
 [4 1]
 [3 2]]
b:
[[0 0 0 0]
 [0 0 0 0]
 [0 0 0 0]]
<type 'numpy.int32'>
a:
[[1 1 1]
 [1 1 1]
 [1 1 1]
 [1 1 1]
 [1 1 1]
 [1 1 1]]
<type 'numpy.int8'>