1

在链接https://zetcode.com/python/httpx/中,它具有以下流示例

import httpx

url = 'https://download.freebsd.org/ftp/releases/amd64/amd64/ISO-IMAGES/12.0/FreeBSD-12.0-RELEASE-amd64-mini-memstick.img'

with open('FreeBSD-12.0-RELEASE-amd64-mini-memstick.img', 'wb') as f:
    with httpx.stream('GET', url) as r:
        for chunk in r.iter_bytes():
            f.write(chunk)

它是一种异步传输数据的方法吗?例如

async def stream(call_back):
   async with httpx.stream('GET', url) as r:
       for chunk in await? r.iter_bytes():
           await call_back(chunk)
4

1 回答 1

1

这应该做的工作,

async def stream(cb):
    async with httpx.AsyncClient() as client:
        async with client.stream('GET', url) as resp:
            async for chunk in resp.aiter_bytes():
                await cb(chunk)

问题是每个块都非常小,比如 3K 字节。

于 2021-11-24T23:42:55.350 回答