Trying to understand Python Concurrent.Futures module

I am trying to figure out how to use the concurrent.futures module in Python 3.2.2, and have played with examples from the documentation. When I try to apply my understanding, my own examples fail. I hope someone can help me hit the road!

I want to be able to install multiple processes simultaneously, but asynchronously. My processes do not return anything. To simulate this, I wrote a trivial example:

 import concurrent.futures fred = [1,2,3,4,5,6,7,8,9,10] def f(x): print(x * x) def main(): with concurrent.futures.ProcessPoolExecutor() as executor: for num in fred: executor.submit(f, num) if __name__ == "__main__": main() 

This code is launched (in the quad-core window of Windows XP) and returns:

1 4 9 16 25

... but then freezes.

Itโ€™s clear that I am doing something wrong. So what is the right way to start Python processes in a process pool? I do not want to use the executor.map approach because I have no return from my process.

Or ... do I need to fake it by returning a True or False process (or something)?

Thanks!

+4
source share
1 answer

I donโ€™t quite understand why you donโ€™t want to use executor.map ... I tried it with a function that returned nothing (with your f function, actually), and it worked fine ...

now when you run it with map , it will not actually print the values, but here is the version of your f function that prints the values:

 import concurrent.futures fred = [1,2,3,4,5,6,7,8,9,10] def f(x): return x * x with concurrent.futures.ProcessPoolExecutor() as executor: for num in executor.map(f, fred): print(num) 
+2
source

All Articles