如何以非阻塞的方式连结期货?也就是说,如何在不阻挡的情况下将一个未来用作另一个未来的输入?

时间:2017-08-28 15:48:02

标签: python python-3.x multiprocessing 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)

1 个答案:

答案 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_stateasyncio.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)