我每 1 毫秒使用 UDP 套接字将 32 字节数据从实时应用程序发送到 python 实例。发送方配置为发送 1ms 时间分辨率 UDP 数据包。在接收端,每隔几次迭代后,就会出现 15 或 16 毫秒的延迟。谁能帮我理解为什么?使用 Windows 虚拟机。Intel Xeon Gold 5120 2 核 CPU,2.20 GHz,6 GB RAM,采用 Windows 10 Pro 操作系统。
## 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))
打印输出如下:
我尝试使用wireshark,可以看到数据包以1ms 的间隔传入。所以基本上 python 套接字可能存在一些缓冲问题。
经过进一步调查发现,14-16 个 UDP 数据包几乎同时进入 python 环境(它们之间的延迟为 0 毫秒),然后在 14-16 毫秒后,下一批数据包到来。就好像有某种缓冲区一样。
30秒到达战场
相关分类