Skip to content

Instantly share code, notes, and snippets.

@nvie
Created August 28, 2012 21:30
Show Gist options
  • Save nvie/3504535 to your computer and use it in GitHub Desktop.
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.
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