V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
anzu
V2EX  ›  问与答

关于下载字节流的问题

  •  
  •   anzu · Sep 29, 2020 · 924 views
    This topic created in 2044 days ago, the information mentioned may be changed or developed.

    举个 python 的例子,其它语言应该差不多。

    async def example():
        chunk_size = 1024
        async with aiohttp.ClientSession() as session:
            async with session.get('example_url') as resp:
                with open('filename', 'wb') as fd:
                    while True:
                        chunk = await resp.content.read(chunk_size)
                        if not chunk:
                            break
                        fd.write(chunk)
                        # do something
                        pass
    

    我理解是程序只有在 read 的时候才会去请求远程数据,而不是一次性下载到内存

    那么问题来了,如果 fd.write 或后面 do something 的阻塞时间较长,远程服务器多久会断开链接?这取决于什么因素?

    是否可以利用 do something 的阻塞时间做下载限流?

    1 replies    2020-09-29 12:34:48 +08:00
    unixeno
        1
    unixeno  
       Sep 29, 2020 via Android
    read 是从 buffer 里 read
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2536 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 05:04 · PVG 13:04 · LAX 22:04 · JFK 01:04
    ♥ Do have faith in what you're doing.