Skip to content

Instantly share code, notes, and snippets.

@dpfrakes
Created October 11, 2018 11:18
Show Gist options
  • Save dpfrakes/7836c6ffa662dad960d112ec39cd052e to your computer and use it in GitHub Desktop.
Save dpfrakes/7836c6ffa662dad960d112ec39cd052e to your computer and use it in GitHub Desktop.
Simple multithreading implementation
"""
https://stackoverflow.com/questions/2846653/how-to-use-threading-in-python
https://stackoverflow.com/questions/5442910/python-multiprocessing-pool-map-for-multiple-arguments/5443941#5443941
"""
import itertools
from multiprocessing import Pool
def _func_run_task(x):
return do_task(*x)
def do_task(i, options):
print options['arg1']
print options['arg2']
return pow(i, 2)
pool = Pool()
arg_dict = {'arg1': 'value1', 'arg2': 'value2'}
results = pool.map(_func_run_task, itertools.izip(range(5), itertools.repeat(arg_dict)))
assert results == [0, 1, 4, 9, 16], 'Results should be returned from do_task in order'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment