Using the below example, how can future2
use the result of future1
once future1
is complete (without blocking future3
from being submitted)?
from concurrent.futures import ProcessPoolExecutor
import time
def wait(seconds):
time.sleep(seconds)
return seconds
pool = ProcessPoolExecutor()
s = time.time()
future1 = pool.submit(wait, 5)
future2 = pool.submit(wait, future1.result())
future3 = pool.submit(wait, 10)
time_taken = time.time() - s
print(time_taken)
This is achievable by carefully crafting a callback to submit the second operation after the first one has completed. Sadly, it is not possible to pass an arbitrary future to
pool.submit
so an extra step is required to bind the two futures together.Here is a possible implementation:
Note that
copy_future_state
is a slightly modified version of asyncio.futures._set_concurrent_future_state.Usage: