0

I have one program sending data with 10Hz frequency.

import socket
import time


if __name__ == '__main__':
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.bind(('127.0.0.1', 19080))

    while True:
        data = time.time()
        sock.sendto(str(data).encode(), ('127.0.0.1', 9090))
        time.sleep(0.1)

And second program recieving data, with delay (1Hz):

import socket
import time


if __name__ == '__main__':
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.bind(('127.0.0.1', 9090))

    while True:
        data = time.time()
        print(time.time(), sock.recv(100))
        time.sleep(1)

After a while, output is:

1600859111.7595737 b'1600858988.4863389'
1600859112.760249 b'1600858988.5863452'
1600859113.760647 b'1600858988.6864707'
1600859114.761207 b'1600858988.7871313'
1600859115.761991 b'1600858988.8875835'

You can see big diffrence in times when data is received(right) and when is send(left). Why it's so much data buffered, and how can I get rid of this? I want possible newest frames, not the buffered ones.

rozumir
  • 845
  • 9
  • 20

2 Answers2

1

Set the socket to non-blocking mode:

sock.setblocking(False)

Then, call sock.recv over and over until you get this error:

while True:
    try:
        msg = sock.recv(100)
        print(time.time(), msg)
    except socket.error as e:
        if e.args[0] not in (errno.EAGAIN, errno.EWOULDBLOCK):
            raise
        break
# we get to this point when there's nothing else to receive.
# Maybe you sleep here, or set it to blocking mode and receive again, or something.

This gives you the advantage that you will read all the messages in the buffer. That also means you'll be able to see the newest message in the buffer. Your solution causes the OS to discard newer messages until you receive the oldest one.

user253751
  • 57,427
  • 7
  • 48
  • 90
  • thank you for yours answer, but `time.sleep()` in my code is simulating computing that takes some time, and I don't need obsolete data from socket, I only want the newest one, so receving all data from buffer no make sens for my purposes. Sorry that I didn't mension it clearly in main post. – rozumir Sep 25 '20 at 06:25
  • @rozumir Receiving all the data makes sense so that you get the newest one. Otherwise, you get the oldest one, because the OS ignores the newer ones because they don't fit in the buffer. – user253751 Sep 25 '20 at 09:32
0

I found the anserw. I had to set socket option for the size of the input buffer.

   sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 0)

Set it to 0, for max effinency.

rozumir
  • 845
  • 9
  • 20