Created
March 8, 2013 19:54
-
-
Save minrk/5119333 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "AsyncOutput" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"from IPython import parallel" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"rc = parallel.Client()\n", | |
"dview = rc[:]\n", | |
"def ff(x):\n", | |
" print(x)\n", | |
" return x**2\n", | |
"sync = dview.map_sync(ff,[1,2,3,4])\n", | |
"print('sync res=%r' % sync)\n", | |
"async = dview.map_async(ff,[1,2,3,4])\n", | |
"print('async res=%r' % async)\n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"sync res=[1, 4, 9, 16]\n", | |
"async res=<AsyncMapResult: ff>\n" | |
] | |
} | |
], | |
"prompt_number": 2 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Wait for the asyncresult to finish" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"async.get()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 3, | |
"text": [ | |
"[1, 4, 9, 16]" | |
] | |
} | |
], | |
"prompt_number": 3 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"`AsyncResult.stdout` is a list of strings, where each string is a single engine's stdout" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"async.stdout" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 4, | |
"text": [ | |
"['1\\n2\\n', '3\\n4\\n']" | |
] | |
} | |
], | |
"prompt_number": 4 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"`display_outputs()` actually prints / displays the output of the engines.\n", | |
"It does not return anything." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"print('async output:')\n", | |
"async.display_outputs()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"async output:\n", | |
"[stdout:0] \n", | |
"1\n", | |
"2\n", | |
"[stdout:1] \n", | |
"3\n", | |
"4\n" | |
] | |
} | |
], | |
"prompt_number": 5 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"We can even make a simple table, showing stdout of each engine side-by-side" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"split_output = [ stdout.splitlines() for stdout in async.stdout ]\n", | |
"transposed = zip(*split_output)\n", | |
"\n", | |
"header = '\\t'.join(\"[%i]\" %i for i in dview.targets)\n", | |
"print(header)\n", | |
"print('-' * 8 * len(dview))\n", | |
"\n", | |
"for row in zip(*split_output):\n", | |
" print('\\t'.join(' %s ' % s for s in row))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[0]\t[1]\n", | |
"----------------\n", | |
" 1 \t 3 \n", | |
" 2 \t 4 \n" | |
] | |
} | |
], | |
"prompt_number": 6 | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment