使用以下示例,future2
完成后future1
如何使用future1
的结果(未阻止future3
提交)?
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)
答案 0 :(得分:4)
通过精心设计回调以在第一个操作完成后提交第二个操作,可以实现这一点。可悲的是,不可能将任意的未来传递给pool.submit
,因此需要额外的步骤来将两个期货绑定在一起。
这是一个可能的实现:
import concurrent.futures
def copy_future_state(source, destination):
if source.cancelled():
destination.cancel()
if not destination.set_running_or_notify_cancel():
return
exception = source.exception()
if exception is not None:
destination.set_exception(exception)
else:
result = source.result()
destination.set_result(result)
def chain(pool, future, fn):
result = concurrent.futures.Future()
def callback(_):
try:
temp = pool.submit(fn, future.result())
copy = lambda _: copy_future_state(temp, result)
temp.add_done_callback(copy)
except:
result.cancel()
raise
future.add_done_callback(callback)
return result
请注意,copy_future_state
是asyncio.futures._set_concurrent_future_state的略微修改版本。
用法:
from concurrent.futures import ProcessPoolExecutor
def wait(seconds):
time.sleep(seconds)
return seconds
pool = ProcessPoolExecutor()
future1 = pool.submit(wait, 5)
future2 = chain(pool, future1, wait)
future3 = pool.submit(wait, 10)