Created
August 28, 2012 21:30
-
-
Save nvie/3504535 to your computer and use it in GitHub Desktop.
The following sample shows a pool of 5 child processes and submits calculations of the Fibonacci numbers 10..40 (this takes looooong). What you can see is that all work is submitted (as `apply_async` doesn't block), instead of blocking after the fifth.
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
from multiprocessing import Pool | |
def fib(n): | |
if n <= 1: | |
return n | |
else: | |
return fib(n - 1) + fib(n - 2) | |
def done(*args, **kwargs): | |
print '==>', args, kwargs | |
p = Pool(5) | |
for x in range(10, 40): | |
p.apply_async(fib, args=(x,), callback=done) | |
print 'Waiting to finish work...' | |
p.close() | |
p.join() | |
print 'Done. Ciao.' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment