天天看點

Python程式設計:aiohttp和requests網絡io性能比較

使用4 種方式 對網絡發起10次請求,進行10次耗時測試

以下代碼在 

Python

3.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,較少握手連接配接次數是很有必要的,就算是單線程請求,也能得到很好地細性能提升