from IPython import parallel
rc = parallel.Client()
dview = rc[:]
def ff(x):
print(x)
return x**2
sync = dview.map_sync(ff,[1,2,3,4])
print('sync res=%r' % sync)
async = dview.map_async(ff,[1,2,3,4])
print('async res=%r' % async)
sync res=[1, 4, 9, 16] async res=<AsyncMapResult: ff>
Wait for the asyncresult to finish
async.get()
[1, 4, 9, 16]
AsyncResult.stdout
is a list of strings, where each string is a single engine's stdout
async.stdout
['1\n2\n', '3\n4\n']
display_outputs()
actually prints / displays the output of the engines.
It does not return anything.
print('async output:')
async.display_outputs()
async output: [stdout:0] 1 2 [stdout:1] 3 4
We can even make a simple table, showing stdout of each engine side-by-side
split_output = [ stdout.splitlines() for stdout in async.stdout ]
transposed = zip(*split_output)
header = '\t'.join("[%i]" %i for i in dview.targets)
print(header)
print('-' * 8 * len(dview))
for row in zip(*split_output):
print('\t'.join(' %s ' % s for s in row))
[0] [1] ---------------- 1 3 2 4