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
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
# 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