Skip to content

Instantly share code, notes, and snippets.

@andreasvc
Created November 11, 2013 14:18
Show Gist options
  • Save andreasvc/7413818 to your computer and use it in GitHub Desktop.
Save andreasvc/7413818 to your computer and use it in GitHub Desktop.
A simple multiprocessing example with process pools, shared data and per-process initialization.
""" A simple multiprocessing example with process pools, shared data and
per-process initialization. """
import multiprocessing
# global read-only data can be shared by each process
DATA = 11
def initworker(a):
""" Initialize data specific to each process. """
global MOREDATA
MOREDATA = a
def worker(args):
""" Function to compute a result in a worker process. """
a, b = args
return DATA / MOREDATA + a * b
if __name__ == '__main__':
numproc = None # None=use all available cores
pool = multiprocessing.Pool(numproc, initworker, (42, ))
work = [(a, b) for a, b in zip(range(5), range(5))]
results = pool.map(worker, work)
print results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment