Python asyncio ensure_future decorator

时间:2017-12-18 21:05:06

标签: python python-3.x python-3.6 python-asyncio

我们假设我是asyncio的新手。我正在使用async / await来并行化我当前的项目,并且我发现自己将所有的协程传递给asyncio.ensure_future。很多像这样的东西:

coroutine = my_async_fn(*args, **kwargs)
task = asyncio.ensure_future(coroutine)

我真正喜欢的是调用异步函数来返回执行任务而不是空闲协程。我创建了一个装饰器来完成我想要做的事情。

def make_task(fn):
    def wrapper(*args, **kwargs):
        return asyncio.ensure_future(fn(*args, **kwargs))
    return wrapper

@make_task
async def my_async_func(*args, **kwargs):
    # usually making a request of some sort
    pass

asyncio是否有内置的方法来执行此操作我无法找到?如果我开始导致这个问题,我是否使用asyncio错误?

2 个答案:

答案 0 :(得分:2)

  

asyncio是否有内置的方式来做到这一点我无法做到   找到?

不,asyncio没有装饰器将协程函数转换为任务。

  

如果我开始导致此问题,我是否使用asyncio错误?

如果没有看到你正在做的事情,很难说,但我认为这可能是真的。虽然在asyncio程序中创建任务是常见的操作,但我怀疑你是否创建了这么多应该是任务的协同程序。

等待协同程序 - 是一种以异步方式调用某些函数"的方法,但阻止当前执行流程直到完成:

await some()

# you'll reach this line *only* when some() done 
另一方面,

任务 - 是一种运行函数in background"的方法,它不会阻止当前的执行流程:

task = asyncio.ensure_future(some())

# you'll reach this line immediately

当我们编写asyncio程序时,我们通常需要第一种方法,因为在开始下一个程序之前我们通常需要一些操作的结果:

text = await request(url)

links = parse_links(text)  # we need to reach this line only when we got 'text'
另一方面,创建任务通常意味着遵循进一步的代码并不依赖于任务的结果。但同样,它并不总是发生。

由于ensure_future会立即返回,有些人会尝试使用它作为一种方式来运行一些协同程序:

# wrong way to run concurrently:
asyncio.ensure_future(request(url1))
asyncio.ensure_future(request(url2))
asyncio.ensure_future(request(url3))

实现此目的的正确方法是使用asyncio.gather

# correct way to run concurrently:
await asyncio.gather(
    request(url1),
    request(url2),
    request(url3),
)

这可能是你想要的吗?

<强> UPD:

我认为在你的案例中使用任务是个好主意。但是我不认为你应该使用装饰器:协程功能(提出请求)仍然是它的具体使用细节(它将用作任务)的一个单独部分。如果请求同步控制与它们的主要功能分开,那么将同步移动到单独的功能中也是有意义的。我会做这样的事情:

import asyncio


async def request(i):
    print(f'{i} started')
    await asyncio.sleep(i)
    print(f'{i} finished')
    return i


async def when_ready(conditions, coro_to_start):
    await asyncio.gather(*conditions, return_exceptions=True)
    return await coro_to_start


async def main():
    t = asyncio.ensure_future

    t1 = t(request(1))
    t2 = t(request(2))
    t3 = t(request(3))
    t4 = t(when_ready([t1, t2], request(4)))
    t5 = t(when_ready([t2, t3], request(5)))

    await asyncio.gather(t1, t2, t3, t4, t5)


if __name__ ==  '__main__':
    loop = asyncio.get_event_loop()
    try:
        loop.run_until_complete(main())
    finally:
        loop.run_until_complete(loop.shutdown_asyncgens())
        loop.close()

答案 1 :(得分:2)

asyncio在很早发布的版本中有@task装饰器,但是我们删除了它。

原因是装饰者不知道要使用什么循环。 asyncio不会在导入时实例化循环,而且为了测试隔离,测试套件通常会为每个测试创建一个新循环。