当前位置 : 主页 > 编程语言 > python >

异步http请求aiohttp的学习(关于同步的requests.get和requests.post的异步应用)

来源:互联网 收集:自由互联 发布时间:2022-06-15
首先需要导入aiohttp模块 import aiohttp 其次通过aiohttp.ClientSession()来代替requests async with aiohttp.ClientSession() as session: # requests async with session.get(url) as resp: # resp = requests.get() 执行完上述操作后


首先需要导入aiohttp模块

import aiohttp

其次通过aiohttp.ClientSession()来代替requests

async with aiohttp.ClientSession() as session: # requests
async with session.get(url) as resp: # resp = requests.get()

执行完上述操作后即可获得所请求的url的内容

使用异步操作时,需要注意的是与同步操作的细微差异

- 1.异步的await resp.content.read() <==>同步的resp.content()
- 2.异步的需要await挂起, resp.text() <==>同步的resp.text


注意:在执行异步操作的列表执行时需要用到await asyncio.wait(列表名称)



上一篇:pytorch的学习和使用
下一篇:没有了
网友评论