为什么aiohttp的运行速度比run_in_executor包装的请求要慢?

时间:2019-11-22 14:43:42

标签: python python-requests aiohttp python-asyncio

全部!

我需要向Web服务发出大约10,000个请求,并且我期望JSON作为响应。由于请求彼此独立,因此我想并行运行它们。我认为aiohttp可以帮助我。我写了以下代码:

import asyncio
import aiohttp


async def execute_module(session: aiohttp.ClientSession, module_id: str,
                         post_body: dict) -> dict:
    headers = {
        'Content-Type': r'application/json',
        'Authorization': fr'Bearer {TOKEN}',
    }

    async with session.post(
            fr'{URL}/{module_id}/steps/execute',
            headers=headers,
            json=post_body,
    ) as response:
        return await response.json()


async def execute_all(campaign_ids, post_body):
    async with aiohttp.ClientSession() as session:
        return await asyncio.gather(*[
            execute_module(session, campaign_id, post_body)
            for campaign_id in campaign_ids
        ])

campaign_ids = ['101', '102', '103'] * 400
post_body = {'inputs': [{"name": "one", "value": 1}]}

print(asyncio.run(execute_all(campaign_ids, post_body)))

PS 。我提出了1200个测试请求。

另一种解决方法-将requests.post包装在run_in_executor函数中。我知道在异步功能中使用阻塞代码是错误的,但它的工作速度更快(〜7秒,而aiohttp为〜10秒)

import requests
import asyncio


def execute_module(module_id, post_body):
    headers = {
        'Content-Type': r'application/json',
        'Authorization': fr'Bearer {TOKEN}',
    }

    return requests.post(
        fr'{URL}/{module_id}/steps/execute',
        headers=headers,
        json=post_body,
    ).json()

async def execute_all(campaign_ids, post_body):
    loop = asyncio.get_running_loop()
    return await asyncio.gather(*[
        loop.run_in_executor(None, execute_module, campaign_id, post_body)
        for campaign_id in campaign_ids
    ])

campaign_ids = ['101', '102', '103'] * 400
post_body = {'inputs': [{"name": "one", "value": 1}]}

print(asyncio.run(execute_all(campaign_ids, post_body)))

我在做什么错了?

2 个答案:

答案 0 :(得分:0)

您是否尝试过uvloop-https://github.com/MagicStack/uvloop?这样可以提高aiohttp请求的速度

答案 1 :(得分:0)

loop.run_in_executor(None, ...) 在线程池(多线程)中运行同步代码。事件循环在一个线程中运行代码。
我的猜测是等待 IO 应该没有太大区别,但处理响应(即 json 解码)确实如此。