小编Ans*_*waj的帖子

Asyncio 未并行运行 Aiohttp 请求

我想使用 python 并行运行许多 HTTP 请求。我尝试使用 asyncio 这个名为 aiohttp 的模块。

import aiohttp
import asyncio

async def main():
    async with aiohttp.ClientSession() as session:
        for i in range(10):
            async with session.get('https://httpbin.org/get') as response:
                html = await response.text()
                print('done' + str(i))

loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Run Code Online (Sandbox Code Playgroud)

我期望它并行执行所有请求,但它们是一一执行的。虽然,我后来使用线程解决了这个问题,但我想知道这有什么问题吗?

python python-asyncio aiohttp

5
推荐指数
1
解决办法
3641
查看次数

标签 统计

aiohttp ×1

python ×1

python-asyncio ×1