小编Yan*_*sky的帖子

为什么 aiohttp 的工作速度比 run_in_executor 包装的请求慢?

全部!

我需要向 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, …
Run Code Online (Sandbox Code Playgroud)

python python-requests python-asyncio aiohttp

7
推荐指数
1
解决办法
860
查看次数

标签 统计

aiohttp ×1

python ×1

python-asyncio ×1

python-requests ×1