Created
January 3, 2018 14:20
-
-
Save emakryo/c00e8484c0844fa646ca9fea8350d2cb 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": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"%config Completer.use_jedi = False\n", | |
"import ipyparallel as ipp\n", | |
"\n", | |
"def run(cmd, executable=\"/bin/bash\"):\n", | |
" \"\"\"\n", | |
" cmd is executed by executable.\n", | |
" Both stdout and stderr is flushed into stdout.\n", | |
" Therefore if you want to split them,\n", | |
" explicitly do in cmd (write to file or throw to /dev/null)\"\"\"\n", | |
" import subprocess, sys\n", | |
" with subprocess.Popen(cmd, shell=True, executable=executable,\n", | |
" stdout=subprocess.PIPE, stderr=subprocess.STDOUT) as proc:\n", | |
" for line in proc.stdout:\n", | |
" sys.stdout.write(line)\n", | |
"\n", | |
" return proc.returncode" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# setup\n", | |
"c = ipp.Client()\n", | |
"print(len(c.ids))\n", | |
"lv = c.load_balanced_view()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"cmds = [\"pwd && echo {}\".format(i) for i in range(10)]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"ar = lv.map(run, cmds)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"lv.queue_status()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"ar.status" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"ar.get()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "parallel", | |
"language": "python", | |
"name": "parallel" | |
}, | |
"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.6.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment