Python协程中的并行异步IO

时间:2017-11-08 00:10:29

标签: python python-asyncio aiohttp

简单示例:我需要并行创建两个不相关的HTTP请求。最简单的方法是什么?我希望它是这样的:

async def do_the_job():
    with aiohttp.ClientSession() as session:
        coro_1 = session.get('http://httpbin.org/get')
        coro_2 = session.get('http://httpbin.org/ip')
        return combine_responses(await coro_1, await coro_2)

换句话说,我想启动IO操作并等待其结果,以便它们有效地并行运行。这可以通过asyncio.gather

来实现
async def do_the_job():
    with aiohttp.ClientSession() as session:
        coro_1 = session.get('http://example.com/get')
        coro_2 = session.get('http://example.org/tp')
        return combine_responses(*(await asyncio.gather(coro_1, coro_2)))

接下来,我想要一些复杂的依赖结构。我希望在我拥有所有先决条件时开始操作,并在需要结果时获得结果。这里帮助asyncio.ensure_future从单独的事件循环管理的协程中创建单独的任务:

async def do_the_job():
    with aiohttp.ClientSession() as session:
        fut_1 = asyncio.ensure_future(session.get('http://httpbin.org/ip'))
        coro_2 = session.get('http://httpbin.org/get')
        coro_3 = session.post('http://httpbin.org/post', data=(await coro_2)
        coro_3_result = await coro_3
        return combine_responses(await fut_1, coro_3_result)

为了在我的逻辑流程中使用协同程序实现并行非阻塞IO,我必须使用asyncio.ensure_futureasyncio.gather(实际使用asyncio.ensure_future)?是否有一种不那么“冗长”的方式?

通常开发人员必须考虑协同应该成为单独的任务并使用上述功能来获得最佳性能吗?

在事件循环中使用没有多个任务的协同程序是否有意义?

现实生活中的事件循环任务有多“重”?当然,它们比OS线程或进程“更轻”。我应该在多大程度上争取尽可能少的此类任务?

1 个答案:

答案 0 :(得分:7)

  

我需要并行发出两个不相关的HTTP请求。什么是   最简单的方法吗?

import asyncio
import aiohttp


async def request(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as resp:
            return await resp.text()


async def main():
    results = await asyncio.gather(
        request('http://httpbin.org/delay/1'),
        request('http://httpbin.org/delay/1'),
    )
    print(len(results))


loop = asyncio.get_event_loop()
try:
    loop.run_until_complete(main())
    loop.run_until_complete(loop.shutdown_asyncgens())
finally:
    loop.close()

是的,您可以使用asyncio.gather实现并发或使用asyncio.ensure_future创建任务。

  

接下来,我想要一些复杂的依赖结构?我想要   当我拥有所有先决条件并获得时,开始操作   我需要结果的结果。

虽然您提供的代码可以正常工作,但在不同的协同程序上拆分并发流并将再次使用asyncio.gather会更好:

import asyncio
import aiohttp


async def request(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as resp:
            return await resp.text()


async def get_ip():
    return await request('http://httpbin.org/ip')


async def post_from_get():
    async with aiohttp.ClientSession() as session:
        async with session.get('http://httpbin.org/get') as resp:
            get_res = await resp.text()
        async with session.post('http://httpbin.org/post', data=get_res) as resp:
            return await resp.text()


async def main():
    results = await asyncio.gather(
        get_ip(),
        post_from_get(),
    )
    print(len(results))


loop = asyncio.get_event_loop()
try:
    loop.run_until_complete(main())
    loop.run_until_complete(loop.shutdown_asyncgens())
finally:
    loop.close()
  

通常开发人员必须考虑什么是协同程序才是真的   应该成为单独的任务并使用上述功能来获得   最佳表现?

由于您使用asyncio,您可能希望同时运行一些作业以获得性能,对吧? asyncio.gather是一种说法 - “同时运行这些工作以更快地获得结果”。

如果您不必考虑应该同时运行哪些作业来获得性能,那么使用纯同步代码就可以了。

  

在事件中使用没有多个任务的协同程序是否有意义   循环?

在您的代码中,如果您不需要,则无需手动创建任务:此答案中的两个片段都不使用asyncio.ensure_future。但是内部asyncio不断使用任务(例如,正如您所指出的那样asyncio.gather使用任务本身)。

  

现实生活中的事件循环任务有多“重”?当然,他们是   比OS线程或进程“更轻”。我应该在多大程度上努力   尽可能少的此类任务?

异步程序的主要瓶颈是(几乎总是)网络:你根本不用担心asyncio coroutines / tasks的数量。

相关问题