Created
March 17, 2016 11:20
-
-
Save natl/6fafd842f93f454c616b to your computer and use it in GitHub Desktop.
Small multiprocessing example
This file contains hidden or 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
| from multiprocessing import Queue, Pool | |
| def run_job(val): | |
| x = 0 | |
| for ii in range(val): | |
| x += ii**2 | |
| return x | |
| if __name__ == "__main__": | |
| tasks = [ii for ii in range(100000)] | |
| result_queue = Queue() | |
| pool = Pool(processes=2) | |
| results = [pool.apply_async(run_job, (task,)) for task in tasks] | |
| r = [result.get() for result in results] | |
| print "Results: " + str(sum(r)) | |
| pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment