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

使用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)

计算结果如下

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

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


上一篇:Python编程:实现消息发布/订阅模型


下一篇:Python编程:将驼峰命名转为小写下划线命名