天天看點

異步http請求aiohttp的學習(關于同步的requests.get和requests.post的異步應用)

首先需要導入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(清單名稱)      

繼續閱讀