Last active
February 17, 2017 08:21
-
-
Save RenSys/dca73dd991ffce971714836cb40a5afa to your computer and use it in GitHub Desktop.
spawn multi processors were total is CPU cores - 1 (ensure it doesn't lock the computer)
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
import multiprocessing | |
def increment_value(num): | |
num = num + 1 | |
print 'this is number ->{}'.format(num) | |
return num | |
if __name__ == '__main__': | |
''' | |
Initialize a pool | |
i.e. Number of vCPU -1 (ensure cpu doesn't hold its breath) | |
''' | |
available_cpu_for_workers = multiprocessing.cpu_count() - 1 | |
pool = multiprocessing.Pool(processes=available_cpu_for_workers) | |
# do work | |
rst = pool.map(increment_value, [1, 2, 3, 5]) | |
# singal end of work | |
pool.close() | |
print "wait for jobs to finish" | |
pool.join() | |
# print summary | |
print "complete" | |
print "map return {}".format(sum(rst)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment