Skip to content

Instantly share code, notes, and snippets.

@Midnighter
Created April 1, 2014 19:46
Show Gist options
  • Save Midnighter/9921621 to your computer and use it in GitHub Desktop.
Save Midnighter/9921621 to your computer and use it in GitHub Desktop.
{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"How to interrupt `ipengines` without shutting down the `ipcluster`?"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import os\n",
"import signal\n",
"import time\n",
"\n",
"from IPython.parallel import Client"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Replace `profile=None` and `cluster_id=None` unless you are using the default profile and no cluster-id."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"rc = Client(profile=None, cluster_id=None)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"dv = rc.direct_view()"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"lv = rc.load_balanced_view()"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"pid_map = rc[:].apply_async(os.getpid).get_dict()\n",
"pid_map"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 5,
"text": [
"{0: 17252, 1: 17253, 2: 17263, 3: 17273}"
]
}
],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def snafu(number):\n",
" for t in range(number):\n",
" number ** number\n",
" return True"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"results = lv.map(snafu, [int(1E04)] * 8, block=False, ordered=False)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Check `top` or similar for running engines."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"time.sleep(20.0)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"for engine_id in pid_map:\n",
" os.kill(pid_map[engine_id], signal.SIGINT)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 9
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"for res in results:\n",
" print \"yes\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"ename": "CompositeError",
"evalue": "one or more exceptions from call to method: snafu\n[1:apply]: KeyboardInterrupt: \n[0:apply]: KeyboardInterrupt: \n[2:apply]: KeyboardInterrupt: \n[3:apply]: KeyboardInterrupt: ",
"output_type": "pyerr",
"traceback": [
"[1:apply]: ",
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)\u001b[1;32m<string>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m",
"\u001b[1;32m<ipython-input-6-2614b8d7f0a9>\u001b[0m in \u001b[0;36msnafu\u001b[1;34m(number)\u001b[0m",
"\u001b[1;31mKeyboardInterrupt\u001b[0m: ",
"",
"[0:apply]: ",
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)\u001b[1;32m<string>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m",
"\u001b[1;32m<ipython-input-6-2614b8d7f0a9>\u001b[0m in \u001b[0;36msnafu\u001b[1;34m(number)\u001b[0m",
"\u001b[1;31mKeyboardInterrupt\u001b[0m: ",
"",
"[2:apply]: ",
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)\u001b[1;32m<string>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m",
"\u001b[1;32m<ipython-input-6-2614b8d7f0a9>\u001b[0m in \u001b[0;36msnafu\u001b[1;34m(number)\u001b[0m",
"\u001b[1;31mKeyboardInterrupt\u001b[0m: ",
"",
"[3:apply]: ",
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)\u001b[1;32m<string>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m",
"\u001b[1;32m<ipython-input-6-2614b8d7f0a9>\u001b[0m in \u001b[0;36msnafu\u001b[1;34m(number)\u001b[0m",
"\u001b[1;31mKeyboardInterrupt\u001b[0m: ",
""
]
}
],
"prompt_number": 10
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment