Last active
December 17, 2016 17:20
-
-
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.
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
.ipynb_checkpoints/ |
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
# 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