Skip to content

Instantly share code, notes, and snippets.

@jbaiter
Last active December 18, 2015 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbaiter/5815446 to your computer and use it in GitHub Desktop.
Save jbaiter/5815446 to your computer and use it in GitHub Desktop.
Generic function that applies `func` (which doesn't have to be imported in the module!) for every set of arguments in `args` and `kwargs` concurrently.
def run_multicore(func, m_args, m_kwargs, num_procs=None):
""" Run `func` once for each set of arguments in `m_args` and `m_kwargs`, using
either `num_procs` worker processes or as many as CPU cores are
available.
"""
class Worker(Process):
def __init__(self, func, queue):
super(Worker, self).__init__()
self.func = func
self.queue = queue
def run(self):
for params in iter(self.queue.get, None):
self.func(*params[0], **params[1])
if not num_procs:
num_procs = cpu_count()
# If args or kwargs is neither None or a list, we assume that this argument
# is to be applied every time.
# FIXME: There must be something in itertools that does this in a manner
# less verbose...
if m_args and not isinstance(m_args, list):
m_args = [m_args for x in xrange(len(m_kwargs or []))]
if m_kwargs and not isinstance(m_kwargs, list):
m_kwargs = [m_kwargs for x in xrange(len(m_args or []))]
running = []
queue = Queue()
for i in xrange(num_procs):
w = Worker(func, queue)
running.append(w)
w.start()
for params in zip(m_args, m_kwargs):
queue.put(params)
for i in xrange(num_procs):
queue.put(None)
for worker in running:
worker.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment