1

I am used to C# Task.WhenAll function. I would like to know if there is an equivalent for python's concurrent.futures.Future

I am looking for something like this:

pool = concurrent.futures.ThreadPoolExecutor(2)
future1 = pool.submit(lambda : 1 + 1)
future2 = pool.submit(lambda : 1 + 2)

aggregated_future = when_all([future1, future2])
print aggregated_future.result() # print result as a list [2, 3]
Sam Hartman
  • 6,210
  • 3
  • 23
  • 40
acai
  • 459
  • 4
  • 9
  • something like https://stackoverflow.com/questions/2562757/is-there-a-multithreaded-map-function – Jean-François Fabre Nov 14 '17 at 20:59
  • but be careful: threads may not be what you're looking for, since GIL makes them execute one at a time (no concurrency) – Jean-François Fabre Nov 14 '17 at 21:00
  • I am reading the post you shared. Thanks for the heads up - I plan to use the threadpool to do I/O work – acai Nov 14 '17 at 21:01
  • asyncio.wait and asyncio.gather can do this for asyncio futures. I don't have enough experience with concurrent.futures to know how to do it there, but obviously you could write something fairly quickly. – Sam Hartman Nov 15 '17 at 14:20
  • Thanks @SamHartman, unfortunately I am using Python 2 and asyncio is not available. I could have written my own, but I am not sure how to do it correctly. E.g., I cannot decide which thread the when_all's future should run on. I feel when_all should be a think wrapper, instead of creating its own thread merely waiting the other two to complete. – acai Nov 15 '17 at 14:27

0 Answers0