Asyncio 未并行运行 Aiohttp 请求 [英] Asyncio not running Aiohttp requests in parallel

查看:35
本文介绍了Asyncio 未并行运行 Aiohttp 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to run many HTTP requests in parallel using python. I tried this module named aiohttp with asyncio.

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())

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

I expect it to execute all the requests in parallel, but they are executed one by one. Although, I later solved this using threading, but I would like to know what's wrong with this?

推荐答案

您需要以并发方式发出请求.目前,您有一个由 main() 定义的任务,因此 http 请求以串行方式为该任务运行.

You need to make the requests in a concurrent manner. Currently, you have a single task defined by main() and so the http requests are run in a serial manner for that task.

您也可以考虑使用 asyncio.run() 如果你使用 Python 版本 3.7+ 抽象出事件循环的创建:

You could also consider using asyncio.run() if you are using Python version 3.7+ that abstracts out creation of event loop:

import aiohttp
import asyncio

async def getResponse(session, i):
    async with session.get('https://httpbin.org/get') as response:
        html = await response.text()
        print('done' + str(i))

async def main():
    async with aiohttp.ClientSession() as session:
        tasks = [getResponse(session, i) for i in range(10)] # create list of tasks
        await asyncio.gather(*tasks) # execute them in concurrent manner

asyncio.run(main())

这篇关于Asyncio 未并行运行 Aiohttp 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆