• 【python】爬虫异步网络请求探索async+request与async+aiohttp


    【python】爬虫异步网络请求探索async+request与async+aiohttp

    在学习协程的时候,会有一个疑问,使用协程语法进行异步请求时,比如async + requests,会有用吗?

    其实细想一下就知道,由于requests库是用同步的方式写的,因此async + requests是肯定没用的。

    但是本着实践出真知的思想,顺便复习巩固一下多线程、async、aiohttp的写法,还是手动来验证一下。

    为了规避网络波动等影响,在本地用Flask搭建一个简易的服务器用于测试。

    文章结构如下:


    先放结论:

    • threading + requests 能够并发请求
    • async + requests 不能并发请求
    • async + aiohttp 能并发请求

    因此在进行爬虫的时候,要想加快效率,要么使用threading + requests ,要么就使用async + aiohttp

    初始环境准备

    安装测试所需要的库

    pip install flask
    pip install requets
    pip install aiohttp
    
    • 1
    • 2
    • 3

    在任意路径创建一个文件夹(文件夹名随意),例如./async_test

    在该文件夹下创建一个空的py文件app.py用于后续搭建测试用后端。

    再创建3个py文件分别对应3个实验,创建完毕后文件目录结构如下(此时的py文件都是空的)

    |- async_test
      |- app.py
      |- 1_threading_requests.py
      |- 2_async_requests.py
      |- 3_async_aiohttp.py
    
    • 1
    • 2
    • 3
    • 4
    • 5

    搭建测试用的后端

    让每次请求的时候先沉睡2秒,再返回结果,以此来模拟网络延迟。

    app.py文件中添加如下代码

    ## app.py ##
    from flask import Flask
    import time
    
    app = Flask(__name__)
    
    @app.route("/")
    def index():
        time.sleep(2)
        return "Hello World!"
    
    if __name__ == '__main__':
        app.run()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    ./async_test目录下运行

    python app.py
    
    • 1
     * Serving Flask app "app" (lazy loading)
     * Environment: production
       WARNING: This is a development server. Do not use it in a production deployment.
       Use a production WSGI server instead.
     * Debug mode: off
     * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    访问 http://127.0.0.1:5000/ 延迟2秒后会看到Hello World!

    完成这一步就搭建好了测试用后端

    1.threading+requests

    1_threading_requests.py文件中添加如下代码

    ## 1_threading_requests.py ##
    import time
    import threading
    
    import requests
    
    def get(i):
        print(time.strftime('%X'), 'start', i)
        resp = requests.get('http://127.0.0.1:5000/')
        print(time.strftime('%X'), 'end', i)
    
    start = time.perf_counter()
    for i in range(4):
        threading.Thread(target=get, args=(i,)).start()
    print(f'total {time.perf_counter() - start:.2f}s ')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    ./async_test目录下运行

    python 1_threading_requests.py
    
    • 1
    09:23:19 start 0
    09:23:19 start 1
    09:23:19 start 2
    09:23:19 start 3
    09:23:21 end 2
    09:23:21 end 0
    09:23:21 end 3
    09:23:21 end 1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    发现使用多线程的写法是能够并发请求的。

    2.async + requests

    2_async_requests.py文件中添加如下代码

    ## 2_async_requests.py ##
    import time
    import asyncio
    
    import requests
    
    async def get(i):
        print(time.strftime('%X'), 'start', i)
        resp = requests.get('http://127.0.0.1:5000/')
        print(time.strftime('%X'), 'end', i)
        
    async def main():
        for i in range(4):
            asyncio.create_task(get(i))
            
    asyncio.run(main())
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    ./async_test目录下运行

    python 2_async_requests.py
    
    • 1
    09:27:11 start 0
    09:27:13 end 0
    09:27:13 start 1
    09:27:15 end 1
    09:27:15 start 2
    09:27:17 end 2
    09:27:17 start 3
    09:27:19 end 3
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    发现async+requests的写法,代码是顺序执行的,异步并没有起到效果

    于是将get(i)函数用aiohttp重写

    3.async + aiohttp

    3_async_aiohttp.py文件中添加如下代码

    ## 3_async_aiohttp.py ##
    import time
    import asyncio
    import aiohttp
    import requests
    
    async def get(i):
        print(time.strftime('%X'), 'start', i)
        async with aiohttp.ClientSession() as session:
            async with session.get('http://127.0.0.1:5000/') as response:
                html = await response.text()
        print(time.strftime('%X'), 'end', i)
    
    async def main():
        tasks = [asyncio.create_task(get(i)) for i in range(4)]
        await asyncio.gather(*tasks)
    
    asyncio.run(main())
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    ./async_test目录下运行

    python 3_async_aiohttp.py
    
    • 1
    09:37:43 start 0
    09:37:43 start 1
    09:37:43 start 2
    09:37:43 start 3
    09:37:45 end 0
    09:37:45 end 2
    09:37:45 end 3
    09:37:45 end 1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    发现代码成功异步执行了,总耗时只有两秒

    说明python的协程语法需要配合异步python库才会生效。

  • 相关阅读:
    win下命名管道通信压力测试实现(含c++示例代码)
    Delay问题分析
    AdaptFormer学习笔记
    ubuntu 中使用Qt连接MMSQl,报错libqsqlodbc.so: undefined symbol: SQLAllocHandle
    STM32 定时器介绍--基本定时器
    SSM - Springboot - MyBatis-Plus 全栈体系(十六)
    postgresql 创建listen notify .net core6.0监听连接
    数据结构之手撕链表(讲解➕源代码)
    构建全真互联数字地图底座 腾讯地图产业版WeMap重磅升级
    由电阻电容采购引发的思考
  • 原文地址:https://blog.csdn.net/Light2077/article/details/127440915