Skip to content

Instantly share code, notes, and snippets.

@lanbugs
Created June 22, 2018 22:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lanbugs/18b0754ea9d632cf88c9f3653e24bf7b to your computer and use it in GitHub Desktop.
Save lanbugs/18b0754ea9d632cf88c9f3653e24bf7b to your computer and use it in GitHub Desktop.
Multiprocessing with result in python
#!/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