python-3.x - AsyncIO 流写入器/读取器是否需要手动确保发送/接收所有数据?

标签 python-3.x python-asyncio

在处理套接字时,您需要确保所有数据都已发送/接收,因为您在读取时可能会收到不完整的数据 block 。来自docs :

In general, they return when the associated network buffers have been filled (send) or emptied (recv). They then tell you how many bytes they handled. It is your responsibility to call them again until your message has been completely dealt with.

强调我的。然后,它展示了确保所有数据都在每个方向上得到处理的示例实现。


在处理套接字上的 AsyncIO 包装器时也是如此吗?

对于 read ,它似乎是必需的,因为文档提到它“[读取] 最多 n 个字节。”。

对于 write不过,好像只要你之后调用drain,你就知道它已经全部发送了。文档没有明确说明必须重复调用它,并且 write 不返回任何内容。

这是正确的吗?我是否需要使用 read 检查读取了多少,但可以只drain StreamWriter 并知道所有内容都已发送?

我认为我上面的假设是正确的,然后我看了一下 example TCP Client紧接在方法文档下方:

import asyncio

async def tcp_echo_client(message):
    reader, writer = await asyncio.open_connection(
        '127.0.0.1', 8888)

    print(f'Send: {message!r}')
    writer.write(message.encode())

    data = await reader.read(100)
    print(f'Received: {data.decode()!r}')

    print('Close the connection')
    writer.close()

asyncio.run(tcp_echo_client('Hello World!'))

而且它不做任何类型的检查。它假定所有内容都是第一次读取和写入。

最佳答案

For read, [checking for incomplete read] seems to be required as the docs mention that it "[reads] up to n bytes.".

正确,这对于许多类型的处理来说都是一个有用的特性,因为它允许您在新数据从对等点到达时读取它并逐步处理它,而不必知道在任何时候期望有多少。如果你确实知道你期望和需要读取多少字节,你可以使用 readexactly .

For write though, it seems like as long as you call drain afterwards, you know that it's all sent. The docs don't explicitly say that it must be called repeatedly, and write doesn't return anything.

这是部分正确的。是的,asyncio 会自动在后台继续写入你给它的数据,直到全部写入,所以你不需要(也不能)通过检查 write 的返回值来确保它。

然而,一系列stream.write(data); await stream.drain() 在所有数据都传输到操作系统之前不会暂停协程。这是因为drain 不会等待所有 数据被写入,它只会等到它达到“低水位线”,试图确保(误导according to some)只要有新的写入,缓冲区就永远不会变空。据我所知,在当前的 asyncio 中,没有办法等到所有数据都已发送 - 除了手动调整水印,这很不方便并且文档警告不要这样做。这同样适用于等待 write() 的返回值引入 in Python 3.8 .

这并不像听起来那么糟糕,因为成功的 write 本身并不能保证数据确实被传输到,更不用说被对等方接收了——它可能在套接字中萎靡不振缓冲区,或沿途的网络设备。但是只要你能依赖系统尽可能快地发送你给它的数据,你并不真正关心其中一些是在异步缓冲区还是在内核缓冲区中。 (但您仍然需要等待 drain() 以确保 backpressure 。)

真正关心的一次是您即将退出程序或事件循环的时间;在这种情况下,一部分数据被卡在 asyncio 缓冲区中意味着对等方将永远看不到它。这就是为什么从3.7开始,asyncio提供了一个wait_closed()方法,你可以在调用close()之后等待,以确保所有数据已经​​发送。可以想象一个 flush() 方法做同样的事情,但不必实际关闭套接字(类似于 file 对象上的同名方法,并且等效语义),但目前没有添加它的计划。

关于python-3.x - AsyncIO 流写入器/读取器是否需要手动确保发送/接收所有数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57683720/

相关文章:

python - 引用子类中的类成员

python - 如何在python中编辑拼接模块的opencv源代码?

python - 取消异步上下文管理器

python - 将 CMD 命令与可执行 python 脚本结合使用

python - B船游戏跟进: How to have the computer randomnize their ships?

python - 嵌套 if 语句在列表列表中不返回任何内容

Python Asyncio 任务取消

python - tkinter 和 asyncio,窗口拖动/调整大小阻止事件循环,单线程

python - @client.event 到底是什么?不和谐.py

python - 为什么 asyncio 使用 aiohttp 发出请求仍然使用线程