Created
June 22, 2018 22:22
-
-
Save lanbugs/18b0754ea9d632cf88c9f3653e24bf7b to your computer and use it in GitHub Desktop.
Multiprocessing with result in python
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
#!/usr/bin/env python | |
# -*- encoding: utf-8; py-indent-offset: 4 -*- | |
import os | |
from multiprocessing import Pool | |
def worker(job): | |
x, y = job | |
result = x ** y | |
return os.getpid(), result | |
if __name__ == '__main__': | |
jobs = [(1, 2), (3, 4), (5, 6), (11, 12), (13, 14), (15, 16), (21, 22), (23, 24), (25, 26)] | |
result_buffer = [] | |
pool = Pool(processes=5) | |
for job in jobs: | |
result_buffer.append(pool.apply_async(worker, args=(job,))) | |
pool.close() | |
pool.join() | |
results = [r.get() for r in result_buffer] | |
print results | |
for pid, result in results: | |
print "working pid was: %s" % pid | |
print "result is: %s" % result | |
print "---" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment