I am sending 32bytes data from a real-time application to a python instance using UDP sockets every 1ms. The sender is configured to send 1ms temporal resolution UDP packets. In the receiving end, after every few iterations, I am having a delay of 15 or 16 ms. Could anyone please help me in understanding why? Using a windows Virtual machine. Intel Xeon Gold 5120 2 core CPU, 2.20 GHz, 6 GB RAM, with Windows 10 Pro OS.
## Import necessary libraries
import socket
import time
"""
just get the raw values from UDP socket every 1ms
The sender sends it with that temporal resolution
"""
UDP_IP = "10.10.114.22"
UDP_PORT = 8208 #UDP phasor values 32 bytes (V,phi,P)
sock_ph = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock_ph.bind((UDP_IP, UDP_PORT))
print("socket bound, waiting for data...")
while True:
time_before_raw = time.monotonic_ns()
raw = sock_ph.recv(32) #I am receiving 32 bytes data
time_after_raw = time.monotonic_ns()
print((time_after_raw-time_before_raw),raw,len(raw))
The printed output is as follows:
I tried with wireshark and could see the data packets coming at 1ms gap. So basically the python socket is probably having some buffering issue.
Upon further investigation it is seen that 14-16 UDP packets are coming in the python environment almost all at once (0 ms delay between them), then after 14-16ms the next batch of packets are coming. It is as if there is some sort of a buffer.
The delay is due to the temporal resolution of monotonic_ns()
, time.perf_counter_ns()
solved this issue.