Python:使用 asyncio.StreamReader.readline() 读取长行

问题描述 投票:0回答:1

readline
() (1) 的异步版本允许从流中异步读取单行。但是,如果遇到长度超过限制的行,则会引发异常 (2)。目前尚不清楚引发此类异常后如何恢复阅读。

我想要一个类似于

readline
() 的函数,它只是丢弃超出限制的部分行,并继续读取直到流结束。有这样的方法吗?如果没有的话怎么写?

1:https://docs.python.org/3/library/asyncio-stream.html#streamreader

2:https://github.com/python/cpython/blob/3.12/Lib/asyncio/streams.py#L549

python asynchronous io python-asyncio
1个回答
0
投票

Asyncio 缓冲区默认限制为

_DEFAULT_LIMIT = 2 ** 16  # 64 KiB

建立连接时可以更改该值:

mylimit = 2 ** 24     #or whatever value you need
async def linereader():
    (reader, _) = await asyncio.open_connection(... limit = mylimit)
    ...
    line = await reader.readline()
    ...
© www.soinside.com 2019 - 2024. All rights reserved.