Skip to content

Instantly share code, notes, and snippets.

@dmmfll
Last active December 17, 2016 17:20
Show Gist options
  • Save dmmfll/1955cf0d8579707e81569e66fddb3b83 to your computer and use it in GitHub Desktop.
Save dmmfll/1955cf0d8579707e81569e66fddb3b83 to your computer and use it in GitHub Desktop.
A way to load a url resource such as a gist into a Jupyter notebook cell.
.ipynb_checkpoints/
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from io import StringIO\n",
"\n",
"# load gist into notebook\n",
"\n",
"gist = ('https://gist.githubusercontent.com/thorsummoner/b5b1dfcff7e7fdd334ec/raw/'\n",
" 'cb6d55b21685df5fade4b2819ea57170b1a42bbd/multiprocessing.py')\n",
"\n",
"text = !curl -s $gist\n",
"fh = StringIO('\\n'.join(text))"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"load fh.read()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# %load fh.read()\n",
"import multiprocessing\n",
"import sys\n",
"\n",
"THREADS = 3\n",
"\n",
"# Used to prevent multiple threads from mixing thier output\n",
"GLOBALLOCK = multiprocessing.Lock()\n",
"\n",
"\n",
"def func_worker(args):\n",
" \"\"\"This function will be called by each thread.\n",
" This function can not be a class method.\n",
" \"\"\"\n",
" # Expand list of args into named args.\n",
" str1, str2 = args\n",
" del args\n",
"\n",
" # Work\n",
" # ...\n",
"\n",
" # Print output\n",
" GLOBALLOCK.acquire()\n",
" print(str1)\n",
" GLOBALLOCK.release()\n",
"\n",
" # Print other output (possibly intermixed)\n",
" GLOBALLOCK.acquire()\n",
" print(str2)\n",
" GLOBALLOCK.release()\n",
"\n",
"\n",
"def main(argp=None):\n",
" \"\"\"Multiprocessing Spawned Example\n",
" \"\"\"\n",
" # Create the number of threads you want\n",
" pool = multiprocessing.Pool(THREADS)\n",
"\n",
" # Define two jobs, each with two args.\n",
" func_args = [\n",
" ('Hello', 'World',), \n",
" ('Goodbye', 'World',), \n",
" ]\n",
"\n",
"\n",
" try:\n",
" # Spawn up to 9999999 jobs, I think this is the maximum possible.\n",
" # I do not know what happens if you exceed this.\n",
" pool.map_async(func_worker, func_args).get(9999999)\n",
" except KeyboardInterrupt:\n",
" # Allow ^C to interrupt from any thread.\n",
" sys.stdout.write('\\033[0m')\n",
" sys.stdout.write('User Interupt\\n')\n",
" pool.close()\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]
},
{
"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
}
# coding: utf-8
# In[1]:
from io import StringIO
# load gist into notebook
gist = ('https://gist.githubusercontent.com/thorsummoner/b5b1dfcff7e7fdd334ec/raw/'
'cb6d55b21685df5fade4b2819ea57170b1a42bbd/multiprocessing.py')
text = get_ipython().getoutput('curl -s $gist')
fh = StringIO('\n'.join(text))
load fh.read()
# In[ ]:
# %load fh.read()
import multiprocessing
import sys
THREADS = 3
# Used to prevent multiple threads from mixing thier output
GLOBALLOCK = multiprocessing.Lock()
def func_worker(args):
"""This function will be called by each thread.
This function can not be a class method.
"""
# Expand list of args into named args.
str1, str2 = args
del args
# Work
# ...
# Print output
GLOBALLOCK.acquire()
print(str1)
GLOBALLOCK.release()
# Print other output (possibly intermixed)
GLOBALLOCK.acquire()
print(str2)
GLOBALLOCK.release()
def main(argp=None):
"""Multiprocessing Spawned Example
"""
# Create the number of threads you want
pool = multiprocessing.Pool(THREADS)
# Define two jobs, each with two args.
func_args = [
('Hello', 'World',),
('Goodbye', 'World',),
]
try:
# Spawn up to 9999999 jobs, I think this is the maximum possible.
# I do not know what happens if you exceed this.
pool.map_async(func_worker, func_args).get(9999999)
except KeyboardInterrupt:
# Allow ^C to interrupt from any thread.
sys.stdout.write('\033[0m')
sys.stdout.write('User Interupt\n')
pool.close()
if __name__ == '__main__':
main()
# In[ ]:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment