如何使用Python的async aiohttp库实现高效的异步网络请求?

2026-04-30 18:151阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计671个文字,预计阅读时间需要3分钟。

如何使用Python的async aiohttp库实现高效的异步网络请求?

目录+前言+初始环境准备+搭建测试用的后端+1. 使用 threading 和 requests+2. 使用 async 和 requests+3. 使用 async 和 aiohttp+前言+在学习协程的时候,会遇到一个疑问,使用协程语法进行异步请求时,例如 async + requests,会有什么问题?

目录
  • 前言
  • 初始环境准备
  • 搭建测试用的后端
  • 1.threading requests
  • 2.async requests
  • 3.async aio127.0.0.1:5000/ (Press CTRL+C to quit)

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

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

    1.threading requests

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

    如何使用Python的async aiohttp库实现高效的异步网络请求?

    ## 1_threading_requests.py ## import time import threading import requests def get(i): print(time.strftime('%X'), 'start', i) resp = requests.get('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('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)函数用aio127.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库才会生效。

    到此这篇关于Pythonasync+request与async+aiohttp实现异步网络请求探索的文章就介绍到这了,更多相关Python异步网络请求内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!

本文共计671个文字,预计阅读时间需要3分钟。

如何使用Python的async aiohttp库实现高效的异步网络请求?

目录+前言+初始环境准备+搭建测试用的后端+1. 使用 threading 和 requests+2. 使用 async 和 requests+3. 使用 async 和 aiohttp+前言+在学习协程的时候,会遇到一个疑问,使用协程语法进行异步请求时,例如 async + requests,会有什么问题?

目录
  • 前言
  • 初始环境准备
  • 搭建测试用的后端
  • 1.threading requests
  • 2.async requests
  • 3.async aio127.0.0.1:5000/ (Press CTRL+C to quit)

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

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

    1.threading requests

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

    如何使用Python的async aiohttp库实现高效的异步网络请求?

    ## 1_threading_requests.py ## import time import threading import requests def get(i): print(time.strftime('%X'), 'start', i) resp = requests.get('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('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)函数用aio127.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库才会生效。

    到此这篇关于Pythonasync+request与async+aiohttp实现异步网络请求探索的文章就介绍到这了,更多相关Python异步网络请求内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!