品易云推流 关闭
文章详情页
文章 > Python基础教程 > python中aiohttp库如何理解

python中aiohttp库如何理解

Python aiohttp

头像

小妮浅浅

2021-10-20 14:41:376156浏览 · 0收藏 · 0评论

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

2、如无需保留请求的对话状态,请求将通过aiohttp.request直接发送回复。

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

实例

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

以上就是python中aiohttp库的理解,希望对大家有所帮助。更多Python学习指路:python基础教程

关注

关注公众号,随时随地在线学习

本教程部分素材来源于网络,版权问题联系站长!

底部广告图