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>
目录
相关文章
|
2月前
|
开发者 Python
深入浅出Python协程:提升IO密集型应用性能
在当今快速发展的软件行业中,提升应用性能已成为开发者追求的重要目标之一。特别是对于IO密集型应用,如何有效利用有限的系统资源以提高并发处理能力,成为了一个值得深入探讨的话题。本文将通过深入浅出的方式,介绍Python协程的基本概念、工作原理及其在提升IO密集型应用性能中的应用。通过具体示例,我们将展示如何使用Python的asyncio库来实现协程,以及如何通过协程优化网络请求和文件操作等IO操作,从而显著提高应用的响应速度和吞吐量。
19 1
|
3月前
|
Java 数据处理
fastdfs源码阅读:文件传输原理与网络IO模型(accept线程、work线程(网络io处理)、dio线程(文件io处理))
fastdfs源码阅读:文件传输原理与网络IO模型(accept线程、work线程(网络io处理)、dio线程(文件io处理))
43 0
|
8月前
|
存储 网络协议 Linux
如何使用io_uring构建快速响应的I/O密集型应用
当涉及构建快速响应的I/O密集型应用时,io_uring技术展现出了其卓越的潜力。本文摘要将深入探讨如何充分利用io_uring的特性来优化应用程序性能。通过异步I/O操作和高效事件处理,io_uring为开发人员提供了一种强大工具,能够显著减少I/O等待时间并实现更高的吞吐量。文章将引导读者了解如何使用io_uring的先进功能,如批量操作和SQPOLL模式,来最大限度地减少系统调用次数,从而降低了开销。同时,我们将探讨如何在不同的应用场景中利用io_uring的灵活性,为数据库、网络服务和存储系统等领域创建出色的性能。通过本文,读者将获得构建高效I/O密集型应用所需的关键见解和实用指南。
142 0
如何使用io_uring构建快速响应的I/O密集型应用
|
5月前
|
网络协议 Linux C++
网络编程-五种IO模型(一)
网络编程-五种IO模型
|
5月前
|
监控 安全 API
网络编程-五种IO模型(二)
网络编程-五种IO模型
|
5月前
|
数据处理
网络编程-五种IO模型(三)
网络编程-五种IO模型
|
8月前
|
存储 JSON 数据格式
【python】IO编程
【python】IO编程
76 0
|
9月前
|
设计模式 监控 NoSQL
【OS面试】说说你对IO多路复用的理解
【OS面试】说说你对IO多路复用的理解
78 0
|
Python
Python编程:aiohttp和requests网络io性能比较
使用4 种方式 对网络发起10次请求,进行10次耗时测试
102 0
Python编程:aiohttp和requests网络io性能比较