知行编程网知行编程网  2022-10-03 11:00 知行编程网 隐藏边栏  26 
文章评分 0 次,平均分 0.0
导语: 本文主要介绍了关于python中aiohttp库如何理解的相关知识,包括python标准库和第三方库的区别,以及aioredis这些编程知识,希望对大家有参考作用。

如何理解python中的aiohttp库

1、aiohttp是一个基于asyncio模块的异步HTTP客户端/服务端框架。

2.如果不需要保持请求的对话状态,请求会直接通过aiohttp.request发送回复。

3. aiohttp 使用字典、列表传递参数或直接通过字符串作为参数传递。


实例

import time
 
import asyncio
import aiohttp
 
 
async def get_html():
    async with aiohttp.request('GET', "https://open.163.com/") as res:
        return await res.text()
 
 
async def main():
    tasks = [asyncio.ensure_future(get_html()) for i in range(20)]
 
    dones, pendings = await asyncio.wait(tasks)
    for task in dones:
        print(len(task.result()))
 
 
if __name__ == '__main__':
    start_time = time.perf_counter()
    asyncio.run(main())
    print("aiohttp 异步采集消耗时间为:", time.perf_counter() - start_time)
    # aiohttp 异步采集消耗时间为: 0.275251032

本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

知行编程网
知行编程网 关注:1    粉丝:1
这个人很懒,什么都没写
扫一扫二维码分享