Python中的asyncio模块中的Future和Task的区别?

问题一

按照官方文档的描述,Task是Futrue的一个subclass,标准库中也分别提供了create_taskcreate_future。请问这两者有功能上的什么区别?

问题二

对于ensure_future不是很理解,官方文档对于它的描述是:

asyncio.ensure_future(coro_or_future, *, loop=None)
Schedule the execution of a coroutine object: wrap it in a future. Return a Task object.If the argument is a Future, it is returned directly.

这段描述中wrap it in a future和Return a Task应该怎么理解,以下面这段代码为例子,factorial(name, number)显然是一个coroutine object,为什么wrap它到一个future对象后又返回一个Task,拜托大神解释下?

import asyncio

@asyncio.coroutine
def factorial(name, number):
    f = 1
    for i in range(2, number+1):
        print("Task %s: Compute factorial(%s)..." % (name, i))
        yield from asyncio.sleep(1)
        f *= i
    print("Task %s: factorial(%s) = %s" % (name, number, f))

loop = asyncio.get_event_loop()
tasks = [
    asyncio.ensure_future(factorial("A", 2)),
    asyncio.ensure_future(factorial("B", 3)),
    asyncio.ensure_future(factorial("C", 4))]
loop.run_until_complete(asyncio.gather(*tasks))
loop.close()

ps: 上面这段代码,如果tasks设置为下面这样,执行效果也是一样的,为什么官方文档的这个例子非要添加一个ensure_futrue,这有什么用途吗?

tasks = [
    factorial("A", 2),
    factorial("B", 3),
    factorial("C", 4)]
阅读 13.8k
2 个回答

第二个问题

简单来说你可以将factorial的print看成一个return.那么,下面的代码就等于在等待factorial的返回.而上面的代码在等待asyncio.gather的返回.而asyncio.gather会等待asyncio.ensure_future调用的任务的结果.asyncio.ensure_future是立即返回的.

在这个例子中当然看不出作用,但是,比如,你已经在进行了factorial("A", 2)的时候,你想调用factorial("B", 3)怎么办,等待factorial("A", 2)的结束吗?那实际上等于同步调用了.所以asyncio.ensure_future可以立即返回,然你不必等待factorial("A", 2)的结果即可运行factorial("A", 2)

新手上路,请多包涵

Future是用来接受异步的结果的。
Task是将future和协程对象组合到一起,用于事件循环的。
我的电脑上跑更改后和更改前的效果是不一样的。顺序相反。相反的原因在于set(coros_or_futures),是不能保证维持list的原有顺序。

你之所以跑出来是一样的,是因为gather函数里是这样

def gather(*coros_or_futures, loop=None, return_exceptions=False):
    """Return a future aggregating results from the given coroutines
    or futures.

    All futures must share the same event loop.  If all the tasks are
    done successfully, the returned future's result is the list of
    results (in the order of the original sequence, not necessarily
    the order of results arrival).  If *return_exceptions* is True,
    exceptions in the tasks are treated the same as successful
    results, and gathered in the result list; otherwise, the first
    raised exception will be immediately propagated to the returned
    future.

    Cancellation: if the outer Future is cancelled, all children (that
    have not completed yet) are also cancelled.  If any child is
    cancelled, this is treated as if it raised CancelledError --
    the outer Future is *not* cancelled in this case.  (This is to
    prevent the cancellation of one child to cause other children to
    be cancelled.)
    """
    if not coros_or_futures:
        if loop is None:
            loop = events.get_event_loop()
        outer = loop.create_future()
        outer.set_result([])
        return outer

    arg_to_fut = {}
    for arg in set(coros_or_futures):
        print('--------------------', arg)
        if not isinstance(arg, futures.Future):
            print('-----------不是一个future-------,就会自动执行ensure_future')            
            fut = ensure_future(arg, loop=loop)
            if loop is None:
                loop = fut._loop
            # The caller cannot control this future, the "destroy pending task"
            # warning should not be emitted.
            fut._log_destroy_pending = False
        else:
            fut = arg
            if loop is None:
                loop = fut._loop
            elif fut._loop is not loop:
                raise ValueError("futures are tied to different event loops")
        arg_to_fut[arg] = fut
    children = [arg_to_fut[arg] for arg in coros_or_futures]
    nchildren = len(children)
    outer = _GatheringFuture(children, loop=loop)
    nfinished = 0
    results = [None] * nchildren
推荐问题
宣传栏