Last active
August 29, 2015 14:00
-
-
Save ping13/11400454 to your computer and use it in GitHub Desktop.
Simple example for a set of workers in Python using a function with multiple arguments (even named ones)
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
# Simple example for a set of workers in Python using a function with multiple | |
# arguments (even named arguments) | |
# Sources of information: | |
# - https://docs.python.org/2/library/multiprocessing.html | |
# - http://stackoverflow.com/questions/5442910/python-multiprocessing-pool-map-for-multiple-arguments/5443941#5443941 | |
from multiprocessing import Pool, freeze_support | |
import itertools | |
def f(x,y,logging = False): | |
if logging: | |
print "calculating %d*%d" % (x,y) | |
return x*y | |
def f_star(x_y): | |
"""Convert `f([1,2])` to `f(1,2)` call.""" | |
return f(x_y[0],x_y[1],logging=x_y[2]) | |
if __name__ == '__main__': | |
freeze_support() # Add support for when a program which uses | |
# multiprocessing has been frozen to produce a Windows | |
# executable. (Has been tested with py2exe, PyInstaller | |
# and cx_Freeze.) | |
pool = Pool(processes=6) # start 4 worker processes | |
print pool.map(f_star, itertools.izip(range(10),range(10,0,-1),itertools.repeat(True))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment