Python编程:aiohttp和requests网络io性能比较

简介: 使用4 种方式 对网络发起10次请求,进行10次耗时测试

使用4 种方式 对网络发起10次请求,进行10次耗时测试


以下代码在 Python3.6.5 下测试

测试代码

# -*- coding: utf-8 -*-

import asyncio
import time

import aiohttp
import requests

urls = ["https://www.baidu.com/"] * 10


# 1、直接使用 requests
def requests_main():
    for url in urls:
        response = requests.get(url)
        html = response.text


# 2、使用 requests.session
def requests_session():
    with requests.session() as session:
        for url in urls:
            response = session.get(url)
            html = response.text


# 3、使用 aiohttp
async def aiohttp_main():
    for url in urls:
        async with aiohttp.ClientSession() as session:
            async with session.get(url) as response:
                html = await response.text()


# 4、 使用 aiohttp.session
async def aiohttp_session():
    async with aiohttp.ClientSession() as session:
        for url in urls:
            async with session.get(url) as response:
                html = await response.text()


if __name__ == '__main__':
    for i in range(10):
        start_time = time.time()
        # requests_main()
        # requests_session()

        # asyncio.get_event_loop().run_until_complete(aiohttp_main())
        asyncio.get_event_loop().run_until_complete(aiohttp_session())

        end_time = time.time()
        print("{:.3}".format(end_time - start_time))

    """
    输出结果:
    
    requests_main
    2.2, 3.69, 2.28, 2.14, 3.37, 2.25, 3.95, 2.97, 2.24, 3.61
    
    requests_session
    0.917, 0.719, 0.682, 0.814, 0.874, 1.66, 0.676, 0.672, 0.66, 0.824
    
    aiohttp_main
    3.1, 2.05, 2.12, 3.12, 1.97, 2.19, 3.38, 2.17, 2.44, 3.2 
    
    aiohttp_session
    1.63, 0.599, 0.656, 0.586, 0.603, 0.607, 0.948, 0.6, 1.54, 1.42 
    
    """

对输出的结果进行平均值计算

requests_main_list = [2.2, 3.69, 2.28, 2.14, 3.37, 2.25, 3.95, 2.97, 2.24, 3.61]

requests_session_list = [0.917, 0.719, 0.682, 0.814, 0.874, 1.66, 0.676, 0.672, 0.66, 0.824]
aiohttp_main_list = [3.1, 2.05, 2.12, 3.12, 1.97, 2.19, 3.38, 2.17, 2.44, 3.2]
aiohttp_session_list = [1.63, 0.599, 0.656, 0.586, 0.603, 0.607, 0.948, 0.6, 1.54, 1.42]

requests_main_avg = sum(requests_main_list) / len(requests_main_list)
requests_session_avg = sum(requests_session_list) / len(requests_session_list)
aiohttp_main_avg = sum(aiohttp_main_list) / len(aiohttp_main_list)
aiohttp_session_avg = sum(aiohttp_session_list) / len(aiohttp_session_list)

print(requests_main_avg)
print(requests_session_avg)
print(aiohttp_main_avg)
print(aiohttp_session_avg)

计算结果如下

6.png

所以,对一个网站请求,最好维护一个session,较少握手连接次数是很有必要的,就算是单线程请求,也能得到很好地细性能提升


            </div>
目录
相关文章
|
3月前
|
数据采集 机器学习/深度学习 人工智能
Python:现代编程的首选语言
Python:现代编程的首选语言
317 102
|
3月前
|
数据采集 机器学习/深度学习 算法框架/工具
Python:现代编程的瑞士军刀
Python:现代编程的瑞士军刀
345 104
|
3月前
|
人工智能 自然语言处理 算法框架/工具
Python:现代编程的首选语言
Python:现代编程的首选语言
274 103
|
5月前
|
存储 Web App开发 前端开发
Python + Requests库爬取动态Ajax分页数据
Python + Requests库爬取动态Ajax分页数据
|
3月前
|
机器学习/深度学习 人工智能 数据挖掘
Python:现代编程的首选语言
Python:现代编程的首选语言
206 82
|
2月前
|
Python
Python编程:运算符详解
本文全面详解Python各类运算符,涵盖算术、比较、逻辑、赋值、位、身份、成员运算符及优先级规则,结合实例代码与运行结果,助你深入掌握Python运算符的使用方法与应用场景。
211 3
|
2月前
|
数据处理 Python
Python编程:类型转换与输入输出
本教程介绍Python中输入输出与类型转换的基础知识,涵盖input()和print()的使用,int()、float()等类型转换方法,并通过综合示例演示数据处理、错误处理及格式化输出,助你掌握核心编程技能。
479 3
|
2月前
|
并行计算 安全 计算机视觉
Python多进程编程:用multiprocessing突破GIL限制
Python中GIL限制多线程性能,尤其在CPU密集型任务中。`multiprocessing`模块通过创建独立进程,绕过GIL,实现真正的并行计算。它支持进程池、队列、管道、共享内存和同步机制,适用于科学计算、图像处理等场景。相比多线程,多进程更适合利用多核优势,虽有较高内存开销,但能显著提升性能。合理使用进程池与通信机制,可最大化效率。
295 3
|
2月前
|
Java 调度 数据库
Python threading模块:多线程编程的实战指南
本文深入讲解Python多线程编程,涵盖threading模块的核心用法:线程创建、生命周期、同步机制(锁、信号量、条件变量)、线程通信(队列)、守护线程与线程池应用。结合实战案例,如多线程下载器,帮助开发者提升程序并发性能,适用于I/O密集型任务处理。
302 0
|
3月前
|
数据采集 机器学习/深度学习 人工智能
Python:现代编程的多面手
Python:现代编程的多面手
92 0

热门文章

最新文章

推荐镜像

更多