亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python中的httpx異步請(qǐng)求

 更新時(shí)間:2024年06月29日 14:32:36   作者:像風(fēng)一樣的男人@  
這篇文章主要介紹了python中的httpx異步請(qǐng)求方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

異步支持

HTTPX默認(rèn)情況下提供標(biāo)準(zhǔn)的同步API,但是如果需要,還可以為你提供異步客戶端的選項(xiàng) 。

要發(fā)出異步請(qǐng)求,你需要一個(gè)httpx.AsyncClient

import asyncio
import httpx

async def main():
    async with httpx.AsyncClient() as client:
        response = await client.get('https://example.org/')

loop = asyncio.get_event_loop()
try:
    loop.run_until_complete(main())
finally:
    loop.close()

發(fā)出請(qǐng)求

AsyncClient.get(url, ...)
AsyncClient.options(url, ...)
AsyncClient.head(url, ...)
AsyncClient.post(url, ...)
AsyncClient.put(url, ...)
AsyncClient.patch(url, ...)
AsyncClient.delete(url, ...)
AsyncClient.request(url, ...)
AsyncClient.send(url, ...)

流式響應(yīng)

Response.aread()
Response.aiter_bytes()
Response.aiter_text()
Response.aiter_lines()
Response.aiter_raw()

實(shí)例

import asyncio
import httpx

async def re():
    async with httpx.AsyncClient() as client:
        res = await client.get('https://www.baidu.com')
        print(res.text)
        return res.text

loop = asyncio.get_event_loop()
task = [re(), ] # 把任務(wù)放入數(shù)組,準(zhǔn)備給事件循環(huán)器調(diào)用
loop.run_until_complete(asyncio.wait(task))
loop.close()

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論