【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

在任意路径创建一个文件夹(文件夹名随意),例如./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

搭建测试用的后端

让每次请求的时候先沉睡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()

./async_test目录下运行

python app.py
 * 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)

访问 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 ')

./async_test目录下运行

python 1_threading_requests.py
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

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

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())

./async_test目录下运行

python 2_async_requests.py
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

发现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())

./async_test目录下运行

python 3_async_aiohttp.py
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

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

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

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值