2

I am running an asyncio TCP server using the asyncio.Protocol class. 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. 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.

EleoFalcon
  • 25
  • 3

1 Answers1

0

In Python 3 you can use the flush keyword argument of print:

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

to forcibly flush the stream. See the documentation.

Jacques Gaudin
  • 15,779
  • 10
  • 54
  • 75