简体   繁体   English

Asyncio服务器不刷新传输缓冲区

[英]Asyncio server not flushing transport buffer

I am running an asyncio TCP server using the asyncio.Protocol class. 我正在使用asyncio.Protocol类运行asyncio TCP服务器。 When I am using the data variable in the data_received() function, I am getting all data that has ever been sent over the socket. 当我在data_received()函数中使用data变量时,我将获取曾经通过套接字发送的所有数据。 Is there a way to flush the buffer every time I read from it? 每当我从缓冲区读取数据时,有没有一种刷新缓冲区的方法? Here is my code: 这是我的代码:

def data_received(self, data):
    data = str(data)
    print("Received Data: ", data)

The only flush function I can find in the docs only works for the StreamWriter class which I am not using. 我可以在文档中找到的唯一刷新函数仅适用于我未使用的StreamWriter类。

In Python 3 you can use the flush keyword argument of print : 在Python 3中,您可以使用printflush关键字参数:

def data_received(self, data):
    data = str(data)
    print("Received Data: ", data, flush=True)

to forcibly flush the stream. 强制冲洗流。 See the documentation . 请参阅文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM