Python异步请求案例
2021/8/11 9:36:32
本文主要是介绍Python异步请求案例,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
import asyncio import aiohttp import time def test(number): start = time.time() async def get(url): session = aiohttp.ClientSession(connector=aiohttp.TCPConnector(limit=64, verify_ssl=False)) response = await session.get(url) await response.text() await session.close() return response async def request(): url = 'https://static1.scrape.center/' await get(url) tasks = [asyncio.ensure_future(request()) for _ in range(number)] loop = asyncio.get_event_loop() loop.run_until_complete(asyncio.wait(tasks)) end = time.time() print('Number:', number, 'Cost time:', end - start) for number in [1, 3, 5, 10]: test(number)
结果如下:
Number: 1 Cost time: 0.4136490821838379 Number: 3 Cost time: 0.17262792587280273 Number: 5 Cost time: 0.17714190483093262 Number: 10 Cost time: 0.16022205352783203
摘自拉勾教育《52讲轻松搞定网络爬虫》之异步爬虫的原理和解析
这篇关于Python异步请求案例的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-03用FastAPI掌握Python异步IO:轻松实现高并发网络请求处理
- 2025-01-02封装学习:Python面向对象编程基础教程
- 2024-12-28Python编程基础教程
- 2024-12-27Python编程入门指南
- 2024-12-27Python编程基础
- 2024-12-27Python编程基础教程
- 2024-12-27Python编程基础指南
- 2024-12-24Python编程入门指南
- 2024-12-24Python编程基础入门
- 2024-12-24Python编程基础:变量与数据类型