千锋教育-做有情怀、有良心、有品质的职业教育机构

pythonaiohttp库如何理解

来源:千锋教育
发布时间:2023-11-11 00:45:50
分享

python中aiohttp库如何理解

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

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

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

实例

importtime

importasyncio

importaiohttp

asyncdefget_html():

asyncwithaiohttp.request('GET',"https://open.163.com/")asres:

returnawaitres.text()

asyncdefmain():

tasks=[asyncio.ensure_future(get_html())foriinrange(20)]

dones,pendings=awaitasyncio.wait(tasks)

fortaskindones:

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相关知识,请关注IT培训机构:千锋教育。

声明:本站部分稿件版权来源于网络,如有侵犯版权,请及时联系我们。

相关推荐

  • pythonsubprocess的用法 python中subprocess的用法1、subprocess这个模块来产生子进程,并且可以连接到子进程的标准输入、输出、错误中,还可以获得子进程的返回值。2、subprocess提供了2种方法调用
  • pythonos.popen方法是什么 pythonos.popen方法是什么1、os.popen方法用于从一个命令打开一个管道。在Unix,Windows中有效。os.popen返回的是一个file对象,同open打开文件一样操作。2、o
  • pythonos.system执行cmd指令 pythonos.system执行cmd指令1、执行cmd指令,在cmd输出的内容会直接在控制台输出,返回结果为0表示执行成功。2、在调用完shell脚本后,返回一个16位的二进制数,低位为杀死所调用
  • pythonif-elif-else语句的使用注意 python中if-elif-else语句的使用注意1、判断多个条件的语句,if为真则执行if后面的语句。2、如果elif是真的,则执行elif,后面的代码块不执行。3、如果if和elif不满意,执行
  • python正则表达式查找和替换内容 1、编写Python正则表达式字符串s。2、使用re.compile将正则表达式编译成正则对象Patternp。3、正则对象p调用p.search或p.findall或p.finditer查找内容。4
  • python正则表达式如何匹配内容 1、编写Python正则表达式字符串s。2、使用re.compile()将正则表达式编译成正则对象Patternp。3、正则对象p调用p.match()或p.fullmatch函数得到匹配对象matc