Python thread pool

It is just a small note about a hidden-ish Python feature. Looking for a thread pool solution, I found the threading module has no support to it.

The most common answers talk about pythonthreadpool library or some ActiveState cookbook recipe. I'm pretty sure it solves the problem, but that is not how Python should work.

If you take a look at multiprocessing.dummy module, you will find it has a Pool implementation using the threading module. I don't know why it is not properly documented, but it replicates multiprocessing pool, so that is exactly what I need.

from multiprocessing.dummy import Pool

def f(x):
    # run something cool here
    return x ** 10

pool = Pool(processes=4)
result = pool.map(f, range(10))
print result

Check multiprocessing.Pool docs.

Comments !

links

social