我每 1 毫秒使用 UDP 套接字將 32 字節(jié)數(shù)據(jù)從實時應(yīng)用程序發(fā)送到 python 實例。發(fā)送方配置為發(fā)送 1ms 時間分辨率 UDP 數(shù)據(jù)包。在接收端,每隔幾次迭代后,就會出現(xiàn) 15 或 16 毫秒的延遲。誰能幫我理解為什么?使用 Windows 虛擬機。Intel Xeon Gold 5120 2 核 CPU,2.20 GHz,6 GB RAM,采用 Windows 10 Pro 操作系統(tǒng)。## Import necessary librariesimport socketimport time"""just get the raw values from UDP socket every 1msThe 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) # UDPsock_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,可以看到數(shù)據(jù)包以1ms 的間隔傳入。所以基本上 python 套接字可能存在一些緩沖問題。 經(jīng)過進一步調(diào)查發(fā)現(xiàn),14-16 個 UDP 數(shù)據(jù)包幾乎同時進入 python 環(huán)境(它們之間的延遲為 0 毫秒),然后在 14-16 毫秒后,下一批數(shù)據(jù)包到來。就好像有某種緩沖區(qū)一樣。
Python UDP套接字,未知延遲
慕田峪4524236
2023-06-27 14:36:43