##// END OF EJS Templates
Update bundled copy of pexpect to 3.3...
Update bundled copy of pexpect to 3.3 Closes gh-6086

File last commit:

r16120:24b93a1d
r17142:2da4f552
Show More
Parallel Magics.ipynb
489 lines | 11.1 KiB | text/plain | TextLexer
MinRK
add parallel magics notebook
r7053 {
"metadata": {
MinRK
update parallel example notebooks to remove `%pylab`
r11537 "name": ""
MinRK
add parallel magics notebook
r7053 },
"nbformat": 3,
MinRK
rebuild example notebooks...
r7739 "nbformat_minor": 0,
MinRK
add parallel magics notebook
r7053 "worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add parallel magics notebook
r7053 "source": [
"Using Parallel Magics"
]
},
{
"cell_type": "markdown",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add parallel magics notebook
r7053 "source": [
MinRK
update Parallel Magics notebook
r7477 "IPython has a few magics for working with your engines.\n",
"\n",
"This assumes you have started an IPython cluster, either with the notebook interface,\n",
MinRK
add parallel magics notebook
r7053 "or the `ipcluster/controller/engine` commands."
]
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
add parallel magics notebook
r7053 "input": [
MinRK
update Parallel Magics notebook
r7477 "from IPython import parallel\n",
"rc = parallel.Client()\n",
"dv = rc[:]\n",
"rc.ids"
MinRK
add parallel magics notebook
r7053 ],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
Olivier Grisel
Add missing import in IPython parallel magics example
r10208 "outputs": []
MinRK
add parallel magics notebook
r7053 },
{
"cell_type": "markdown",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add parallel magics notebook
r7053 "source": [
MinRK
update Parallel Magics notebook
r7498 "Creating a Client registers the parallel magics `%px`, `%%px`, `%pxresult`, `pxconfig`, and `%autopx`. \n",
MinRK
update Parallel Magics notebook
r7477 "These magics are initially associated with a DirectView always associated with all currently registered engines."
MinRK
add parallel magics notebook
r7053 ]
},
{
"cell_type": "markdown",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add parallel magics notebook
r7053 "source": [
"Now we can execute code remotely with `%px`:"
]
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
add parallel magics notebook
r7053 "input": [
"%px a=5"
],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add parallel magics notebook
r7053 "outputs": []
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
add parallel magics notebook
r7053 "input": [
Thomas Kluyver
Update parallel example notebooks for Python 3
r13997 "%px print(a)"
MinRK
add parallel magics notebook
r7053 ],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add parallel magics notebook
r7053 "outputs": []
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
add parallel magics notebook
r7053 "input": [
"%px a"
],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add parallel magics notebook
r7053 "outputs": []
},
{
MinRK
aesthetics pass on AsyncResult.display_outputs...
r7239 "cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
aesthetics pass on AsyncResult.display_outputs...
r7239 "input": [
MinRK
update Parallel Magics notebook
r7477 "with dv.sync_imports():\n",
MinRK
add missing sys import
r7250 " import sys"
],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add missing sys import
r7250 "outputs": []
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
add missing sys import
r7250 "input": [
Thomas Kluyver
Update parallel example notebooks for Python 3
r13997 "%px from __future__ import print_function\n",
"%px print(\"ERROR\", file=sys.stderr)"
MinRK
aesthetics pass on AsyncResult.display_outputs...
r7239 ],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
aesthetics pass on AsyncResult.display_outputs...
r7239 "outputs": []
},
{
MinRK
add parallel magics notebook
r7053 "cell_type": "markdown",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add parallel magics notebook
r7053 "source": [
MinRK
update Parallel Magics notebook
r7477 "You don't have to wait for results. The `%pxconfig` magic lets you change the default blocking/targets for the `%px` magics:"
MinRK
add parallel magics notebook
r7053 ]
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
add parallel magics notebook
r7053 "input": [
MinRK
update Parallel Magics notebook
r7477 "%pxconfig --noblock"
MinRK
add parallel magics notebook
r7053 ],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add parallel magics notebook
r7053 "outputs": []
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
add parallel magics notebook
r7053 "input": [
MinRK
update Parallel Magics notebook
r7477 "%px import time\n",
"%px time.sleep(5)\n",
MinRK
add parallel magics notebook
r7053 "%px time.time()"
],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add parallel magics notebook
r7053 "outputs": []
},
{
"cell_type": "markdown",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add parallel magics notebook
r7053 "source": [
MinRK
update Parallel Magics notebook
r7477 "But you will notice that this didn't output the result of the last command.\n",
MinRK
update Parallel Magics notebook
r7498 "For this, we have `%pxresult`, which displays the output of the latest request:"
MinRK
add parallel magics notebook
r7053 ]
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
add parallel magics notebook
r7053 "input": [
MinRK
update Parallel Magics notebook
r7498 "%pxresult"
MinRK
add parallel magics notebook
r7053 ],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add parallel magics notebook
r7053 "outputs": []
},
{
"cell_type": "markdown",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add parallel magics notebook
r7053 "source": [
"Remember, an IPython engine is IPython, so you can do magics remotely as well!"
]
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
add parallel magics notebook
r7053 "input": [
MinRK
update Parallel Magics notebook
r7477 "%pxconfig --block\n",
MinRK
update parallel example notebooks to remove `%pylab`
r11537 "%px %matplotlib inline"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%px\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt"
MinRK
add parallel magics notebook
r7053 ],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add parallel magics notebook
r7053 "outputs": []
},
{
"cell_type": "markdown",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add parallel magics notebook
r7053 "source": [
MinRK
update Parallel Magics notebook
r7477 "`%%px` can also be used as a cell magic, for submitting whole blocks.\n",
"This one acceps `--block` and `--noblock` flags to specify\n",
"the blocking behavior, though the default is unchanged.\n"
MinRK
add parallel magics notebook
r7053 ]
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
add parallel magics notebook
r7053 "input": [
MinRK
update Parallel Magics notebook
r7477 "dv.scatter('id', dv.targets, flatten=True)\n",
MinRK
add parallel magics notebook
r7053 "dv['stride'] = len(dv)"
],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add parallel magics notebook
r7053 "outputs": []
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
add parallel magics notebook
r7053 "input": [
MinRK
update Parallel Magics notebook
r7477 "%%px --noblock\n",
MinRK
update parallel example notebooks to remove `%pylab`
r11537 "x = np.linspace(0,np.pi,1000)\n",
MinRK
update Parallel Magics notebook
r7477 "for n in range(id,12, stride):\n",
Thomas Kluyver
Update parallel example notebooks for Python 3
r13997 " print(n)\n",
MinRK
update parallel example notebooks to remove `%pylab`
r11537 " plt.plot(x,np.sin(n*x))\n",
MinRK
add parallel magics notebook
r7053 "plt.title(\"Plot %i\" % id)"
],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add parallel magics notebook
r7053 "outputs": []
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
add parallel magics notebook
r7053 "input": [
MinRK
update Parallel Magics notebook
r7498 "%pxresult"
MinRK
add parallel magics notebook
r7053 ],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add parallel magics notebook
r7053 "outputs": []
},
{
"cell_type": "markdown",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add parallel magics notebook
r7053 "source": [
MinRK
update Parallel Magics notebook
r7477 "It also lets you choose some amount of the grouping of the outputs with `--group-outputs`:\n",
"\n",
"The choices are:\n",
"\n",
"* `engine` - all of an engine's output is collected together\n",
"* `type` - where stdout of each engine is grouped, etc. (the default)\n",
"* `order` - same as `type`, but individual displaypub outputs are interleaved.\n",
" That is, it will output the first plot from each engine, then the second from each,\n",
MinRK
add parallel magics notebook
r7053 " etc."
]
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
add parallel magics notebook
r7053 "input": [
MinRK
update Parallel Magics notebook
r7477 "%%px --group-outputs=engine\n",
MinRK
update parallel example notebooks to remove `%pylab`
r11537 "x = np.linspace(0,np.pi,1000)\n",
MinRK
update Parallel Magics notebook
r7477 "for n in range(id+1,12, stride):\n",
Thomas Kluyver
Update parallel example notebooks for Python 3
r13997 " print(n)\n",
MinRK
update Parallel Magics notebook
r7477 " plt.figure()\n",
MinRK
update parallel example notebooks to remove `%pylab`
r11537 " plt.plot(x,np.sin(n*x))\n",
MinRK
update Parallel Magics notebook
r7477 " plt.title(\"Plot %i\" % n)"
MinRK
add parallel magics notebook
r7053 ],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add parallel magics notebook
r7053 "outputs": []
},
{
MinRK
aesthetics pass on AsyncResult.display_outputs...
r7239 "cell_type": "markdown",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
aesthetics pass on AsyncResult.display_outputs...
r7239 "source": [
MinRK
update Parallel Magics notebook
r7477 "When you specify 'order', then individual display outputs (e.g. plots) will be interleaved.\n",
"\n",
MinRK
update Parallel Magics notebook
r7498 "`%pxresult` takes the same output-ordering arguments as `%%px`, \n",
"so you can view the previous result in a variety of different ways with a few sequential calls to `%pxresult`:"
MinRK
aesthetics pass on AsyncResult.display_outputs...
r7239 ]
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
aesthetics pass on AsyncResult.display_outputs...
r7239 "input": [
MinRK
update Parallel Magics notebook
r7498 "%pxresult --group-outputs=order"
MinRK
aesthetics pass on AsyncResult.display_outputs...
r7239 ],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
aesthetics pass on AsyncResult.display_outputs...
r7239 "outputs": []
},
{
"cell_type": "heading",
"level": 2,
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
aesthetics pass on AsyncResult.display_outputs...
r7239 "source": [
"Single-engine views"
]
},
{
"cell_type": "markdown",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
aesthetics pass on AsyncResult.display_outputs...
r7239 "source": [
"When a DirectView has a single target, the output is a bit simpler (no prefixes on stdout/err, etc.):"
]
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
aesthetics pass on AsyncResult.display_outputs...
r7239 "input": [
Thomas Kluyver
Update parallel example notebooks for Python 3
r13997 "from __future__ import print_function\n",
"\n",
MinRK
update Parallel Magics notebook
r7477 "def generate_output():\n",
" \"\"\"function for testing output\n",
" \n",
" publishes two outputs of each type, and returns something\n",
" \"\"\"\n",
" \n",
" import sys,os\n",
MinRK
update parallel example notebooks to remove `%pylab`
r11537 " from IPython.display import display, HTML, Math\n",
MinRK
update Parallel Magics notebook
r7477 " \n",
Thomas Kluyver
Update parallel example notebooks for Python 3
r13997 " print(\"stdout\")\n",
" print(\"stderr\", file=sys.stderr)\n",
MinRK
update Parallel Magics notebook
r7477 " \n",
" display(HTML(\"<b>HTML</b>\"))\n",
" \n",
Thomas Kluyver
Update parallel example notebooks for Python 3
r13997 " print(\"stdout2\")\n",
" print(\"stderr2\", file=sys.stderr)\n",
MinRK
update Parallel Magics notebook
r7477 " \n",
" display(Math(r\"\\alpha=\\beta\"))\n",
" \n",
" return os.getpid()\n",
"\n",
MinRK
aesthetics pass on AsyncResult.display_outputs...
r7239 "dv['generate_output'] = generate_output"
],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
aesthetics pass on AsyncResult.display_outputs...
r7239 "outputs": []
},
{
MinRK
update Parallel Magics notebook
r7477 "cell_type": "markdown",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
update Parallel Magics notebook
r7477 "source": [
"You can also have more than one set of parallel magics registered at a time.\n",
"\n",
"The `View.activate()` method takes a suffix argument, which is added to `'px'`."
]
},
{
MinRK
add parallel magics notebook
r7053 "cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
add parallel magics notebook
r7053 "input": [
MinRK
update Parallel Magics notebook
r7477 "e0 = rc[-1]\n",
"e0.block = True\n",
"e0.activate('0')"
],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
update Parallel Magics notebook
r7477 "outputs": []
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
update Parallel Magics notebook
r7477 "input": [
"%px0 generate_output()"
MinRK
aesthetics pass on AsyncResult.display_outputs...
r7239 ],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
aesthetics pass on AsyncResult.display_outputs...
r7239 "outputs": []
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
aesthetics pass on AsyncResult.display_outputs...
r7239 "input": [
"%px generate_output()"
MinRK
add parallel magics notebook
r7053 ],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add parallel magics notebook
r7053 "outputs": []
MinRK
update Parallel Magics notebook
r7477 },
{
"cell_type": "markdown",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
update Parallel Magics notebook
r7477 "source": [
"As mentioned above, we can redisplay those same results with various grouping:"
]
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
update Parallel Magics notebook
r7477 "input": [
"%pxresult --group-outputs order"
],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
update Parallel Magics notebook
r7477 "outputs": []
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
update Parallel Magics notebook
r7477 "input": [
"%pxresult --group-outputs engine"
],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
update Parallel Magics notebook
r7477 "outputs": []
},
{
MinRK
update Parallel Magics notebook
r7498 "cell_type": "heading",
"level": 2,
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
update Parallel Magics notebook
r7498 "source": [
"Parallel Exceptions"
]
},
{
"cell_type": "markdown",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
update Parallel Magics notebook
r7498 "source": [
"When you raise exceptions with the parallel exception,\n",
"the CompositeError raised locally will display your remote traceback."
]
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
update Parallel Magics notebook
r7498 "input": [
"%%px\n",
"from numpy.random import random\n",
"A = random((100,100,'invalid shape'))"
],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
update Parallel Magics notebook
r7498 "outputs": []
},
{
"cell_type": "heading",
"level": 2,
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
update Parallel Magics notebook
r7498 "source": [
"Remote Cell Magics"
]
},
{
"cell_type": "markdown",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
update Parallel Magics notebook
r7498 "source": [
"Remember, Engines are IPython too, so the cell that is run remotely by %%px can in turn use a cell magic."
]
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
update Parallel Magics notebook
r7498 "input": [
"%%px\n",
"%%timeit\n",
"from numpy.random import random\n",
"from numpy.linalg import norm\n",
"A = random((100,100))\n",
MinRK
update parallel example notebooks to remove `%pylab`
r11537 "norm(A, 2)"
MinRK
update Parallel Magics notebook
r7498 ],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
update Parallel Magics notebook
r7498 "outputs": []
},
{
MinRK
add %%px --local to Parallel Magics notebook
r9022 "cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Local Execution"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Thomas Kluyver
Update parallel example notebooks for Python 3
r13997 "As of IPython 1.0, you can instruct `%%px` to also execute the cell locally.\n",
MinRK
add %%px --local to Parallel Magics notebook
r9022 "This is useful for interactive definitions,\n",
"or if you want to load a data source everywhere,\n",
"not just on the engines."
]
},
{
MinRK
update Parallel Magics notebook
r7477 "cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
add %%px --local to Parallel Magics notebook
r9022 "input": [
"%%px --local\n",
Olivier Grisel
Add missing import in IPython parallel magics example
r10208 "import os\n",
MinRK
add %%px --local to Parallel Magics notebook
r9022 "thispid = os.getpid()\n",
Thomas Kluyver
Update parallel example notebooks for Python 3
r13997 "print(thispid)"
MinRK
add %%px --local to Parallel Magics notebook
r9022 ],
MinRK
update Parallel Magics notebook
r7477 "language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
Olivier Grisel
Add missing import in IPython parallel magics example
r10208 "outputs": []
MinRK
add parallel magics notebook
r7053 }
MinRK
rebuild example notebooks...
r7739 ],
"metadata": {}
MinRK
add parallel magics notebook
r7053 }
]
}