Skip to content

Instantly share code, notes, and snippets.

@jdfreder
Created April 3, 2015 19:48
Show Gist options
  • Save jdfreder/4ae1cfd4b85b91003806 to your computer and use it in GitHub Desktop.
Save jdfreder/4ae1cfd4b85b91003806 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from zmq.eventloop import IOLoop\n",
"\n",
"class NamedControlCallbacks(dict):\n",
" def __init__(self, start=True):\n",
" if start:\n",
" self.start()\n",
" else:\n",
" self.stop()\n",
"\n",
" def start(self):\n",
" self.running = True\n",
" IOLoop.instance().add_callback(self._loop)\n",
" \n",
" def stop(self):\n",
" self.running = False\n",
" \n",
" def do_one_iteration(self):\n",
" for callback in self.values():\n",
" callback()\n",
"\n",
" def _loop(self):\n",
" self.do_one_iteration()\n",
" if self.running:\n",
" IOLoop.instance().add_callback(self._loop)\n",
"\n",
" def __del__(self):\n",
" self.stop()\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"a = NamedControlCallbacks()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"x = 0\n",
"def inc():\n",
" global x\n",
" x += 1\n",
"a['bob'] = inc"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"945759"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"a.stop()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"a.do_one_iteration()"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"a.start()"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"del a['bob']"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"a['bob'] = inc"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"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.3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment