Gre*_*reg 9 python multiprocessing python-3.x python-asyncio concurrent.futures
使用下面的例子,如何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)
Run Code Online (Sandbox Code Playgroud)
这可以通过精心设计回调以在第一个操作完成后提交第二个操作来实现。遗憾的是,不可能将任意未来传递给,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
Run Code Online (Sandbox Code Playgroud)
请注意,这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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1416 次 |
| 最近记录: |