{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "> This is one of the 100 recipes of the [IPython Cookbook](http://ipython-books.github.io/), the definitive guide to high-performance scientific computing and data science in Python.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 5.9. Distributing Python code across multiple cores with IPython" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, we launch 4 IPython engines with `ipcluster start -n 4` in a console." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then, we create a client that will act as a proxy to the IPython engines. The client automatically detects the running engines." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from IPython.parallel import Client\n", "rc = Client()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's check the number of running engines." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "rc.ids" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To run commands in parallel over the engines, we can use the %px magic or the %%px cell magic." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%px\n", "import os\n", "print(\"Process {0:d}.\".format(os.getpid()))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can specify which engines to run the commands on using the --targets or -t option." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%px -t 1,2\n", "# The os module has already been imported in the previous cell.\n", "print(\"Process {0:d}.\".format(os.getpid()))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "By default, the %px magic executes commands in blocking mode: the cell returns when the commands have completed on all engines. It is possible to run non-blocking commands with the --noblock or -a option. In this case, the cell returns immediately, and the task's status and the results can be polled asynchronously from the IPython interactive session." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%px -a\n", "import time\n", "time.sleep(5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The previous command returned an ASyncResult instance that we can use to poll the task's status." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(_.elapsed, _.ready())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The %pxresult blocks until the task finishes." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%pxresult" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(_.elapsed, _.ready())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "IPython provides convenient functions for most common use-cases, like a parallel map function." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "v = rc[:]\n", "res = v.map(lambda x: x*x, range(10))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(res.get())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> You'll find all the explanations, figures, references, and much more in the book (to be released later this summer).\n", "\n", "> [IPython Cookbook](http://ipython-books.github.io/), by [Cyrille Rossant](http://cyrille.rossant.net), Packt Publishing, 2014 (500 pages)." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.4.2" } }, "nbformat": 4, "nbformat_minor": 0 }