concurrent.futures: a simple multi-thread execution

Recently I was expose to a way to launch parallel tasks in python by using concurrent.futures.  Let say you have a function call doSomething(i) that do something with a value, i and return a result. You can setup a pool of 8 threads as following:

executor = concurrent.futures.ThreadPoolExecutor(max_workers=8)

If we need to doSomething(i) on a range(64) values, we can map the result using the executor as follow:

results = executor.map(doSomething, range(64))

This will return an array of 64 result. Be sure to signal the executor to free any resources by having the following line.

executor.shutdown()

Additional References:

Advertisements