Python Multiprocessing Pool Example

This script demonstrates the primary ways to use the `multiprocessing.Pool` class in Python's standard library:

multiprocessing-pool-example.py
#!/usr/bin/python
 
import multiprocessing
import time
 
def square(x):
    # This is is reeeeally slow way to square numbers.
    time.sleep(0.5)
    return x**2
 
print "Creating pool with 3 workers"
pool = multiprocessing.Pool(processes=3)
 
print "Invoking apply(square, 3)"
print "Result: %s" % (pool.apply(square, [3]),)
 
print "Invoking apply_async(square, 4)"
result = pool.apply_async(square, [4])
print "Waiting for result..."
start_time = time.time()
print "Result: %s (%.2f secs)" % (result.get(), time.time() - start_time)
 
print "Invoking map(square, [5,6,7,8,9,10])"
print "Result: %r" % (result.get(),)
print "Invoking map_async(square, [11, 12, 13, 14, 15, 16])"
result = pool.map_async(square, [11, 12, 13, 14, 15, 16])
print "Waiting for result..."
start_time = time.time()
print "Result: %r (%.2f secs)" % (result.get(), time.time() - start_time)
 
print "Invoking apply_async(square, X) once each for X in 17-24"
results = [(i, pool.apply_async(square, [i])) for i in xrange(17, 25)]
start_time = time.time()
for i, result in results:
    print "Result (%d): %s (%.2f secs)" % (i, result.get(), time.time() - start_time)

It generates the following output:

Creating pool with 3 workers
Invoking apply(square, 3)
Result: 9
Invoking apply_async(square, 4)
Waiting for result...
Result: 16 (0.50 secs)
Invoking map(square, [5,6,7,8,9,10])
Result: 16
Invoking map_async(square, [11, 12, 13, 14, 15, 16])
Waiting for result...
Result: [121, 144, 169, 196, 225, 256] (1.00 secs)
Invoking apply_async(square, X) once each for X in 17-24
Result (17): 289 (0.50 secs)
Result (18): 324 (0.50 secs)
Result (19): 361 (0.50 secs)
Result (20): 400 (1.00 secs)
Result (21): 441 (1.00 secs)
Result (22): 484 (1.00 secs)
Result (23): 529 (1.50 secs)
Result (24): 576 (1.50 secs)