Created
March 1, 2016 16:19
-
-
Save DBCerigo/88c0851aa2372b1b3d40 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
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
" * before starting this code, please type \"ipcluster start -n 3\" in the terminal\n", | |
" * `-n 3`: set the number of child processes\n", | |
" * in order to do that, you need to install ipyparallel first. I recommend you to use pip,\n", | |
"```\n", | |
"pip install ipyparallel\n", | |
"```\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 38, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"importing numpy on engine(s)\n", | |
"importing collections on engine(s)\n", | |
"importing networkx on engine(s)\n" | |
] | |
} | |
], | |
"source": [ | |
"from ipyparallel import Client\n", | |
"\n", | |
"# see https://ipyparallel.readthedocs.org/en/latest/multiengine.html\n", | |
"rc = Client()\n", | |
"\n", | |
"# Generate child processes\n", | |
"dview = rc[:]\n", | |
"\n", | |
"# import modules in the child processes\n", | |
"with dview.sync_imports():\n", | |
" import numpy\n", | |
" import collections\n", | |
" import networkx\n", | |
"\n", | |
"dview.block = False" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 32, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def mapper(year):\n", | |
" return year, numpy.sqrt(year)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 33, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"year_list = range(1900, 1905+1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 34, | |
"metadata": { | |
"collapsed": false, | |
"scrolled": true | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[1900, 1901, 1902, 1903, 1904, 1905]\n" | |
] | |
} | |
], | |
"source": [ | |
"print year_list" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 35, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"True" | |
] | |
}, | |
"execution_count": 35, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Run mapper in parallel with N_threads\n", | |
"res = dview.map_async(mapper, year_list)\n", | |
"\n", | |
"dview.wait(res)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 36, | |
"metadata": { | |
"collapsed": false, | |
"scrolled": true | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1900 43.5889894354\n", | |
"1901 43.6004587132\n", | |
"1902 43.6119249747\n", | |
"1903 43.6233882224\n", | |
"1904 43.6348484585\n", | |
"1905 43.6463056856\n" | |
] | |
} | |
], | |
"source": [ | |
"for year, year_sqrt in res.get():\n", | |
" print year, year_sqrt " | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 2", | |
"language": "python", | |
"name": "python2" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.10" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment