测试此 UDP 套接字 Python 代码:奇怪的延迟; Windows 对比 Linux
Test this UDP socket Python Code: Weird delay; Windows vs Linux
我以 1 毫秒的间隔发送数据,但在接收端,我似乎一次收到 15-16 个数据包,然后在 15-16 毫秒延迟后接收另一组数据包。
请尝试此代码,看看您是否有相同的结果。我正在使用 Windows 10 机器。我试过 2 件,结果相同。任何见解将不胜感激。
# -*- coding: utf-8 -*-
"""
Created on Thu Sep 3 23:32:44 2020
@author: Pratyush
"""
# sender.py
import socket
from time import sleep,monotonic_ns
TEMPO = 1e6
send = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
last_second = monotonic_ns()
iteration_count = 0
while iteration_count < 1000:
if monotonic_ns() >= last_second + TEMPO:
last_second += TEMPO
send.sendto(bytes(32), ('127.0.0.1', 8208))
iteration_count += 1
接收代码如下,运行先receiver.py
# -*- coding: utf-8 -*-
"""
Created on Thu Sep 3 23:32:13 2020
@author: Pratyush
"""
# receiver2.py
import socket
import time
"""
just get the raw values from UDP socket every 1ms
The sender sends it with that temporal resolution
"""
UDP_IP = ""
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))
print((time_after_raw-time_before_raw),len(raw))
我收到的结果如下:
0 32
0 32
16000000 32
0 32
0 32
0 32
0 32
0 32
0 32
0 32
0 32
0 32
0 32
0 32
0 32
0 32
0 32
0 32
15000000 32
0 32
0 32
0 32
0 32
0 32
0 32
0 32
0 32
0 32
0 32
0 32
0 32
0 32
0 32
根据 PEP,Windows'单调时钟的分辨率为 15mS。很可能是您在输出中看到的粒度。
我以 1 毫秒的间隔发送数据,但在接收端,我似乎一次收到 15-16 个数据包,然后在 15-16 毫秒延迟后接收另一组数据包。
请尝试此代码,看看您是否有相同的结果。我正在使用 Windows 10 机器。我试过 2 件,结果相同。任何见解将不胜感激。
# -*- coding: utf-8 -*-
"""
Created on Thu Sep 3 23:32:44 2020
@author: Pratyush
"""
# sender.py
import socket
from time import sleep,monotonic_ns
TEMPO = 1e6
send = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
last_second = monotonic_ns()
iteration_count = 0
while iteration_count < 1000:
if monotonic_ns() >= last_second + TEMPO:
last_second += TEMPO
send.sendto(bytes(32), ('127.0.0.1', 8208))
iteration_count += 1
接收代码如下,运行先receiver.py
# -*- coding: utf-8 -*-
"""
Created on Thu Sep 3 23:32:13 2020
@author: Pratyush
"""
# receiver2.py
import socket
import time
"""
just get the raw values from UDP socket every 1ms
The sender sends it with that temporal resolution
"""
UDP_IP = ""
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))
print((time_after_raw-time_before_raw),len(raw))
我收到的结果如下:
0 32
0 32
16000000 32
0 32
0 32
0 32
0 32
0 32
0 32
0 32
0 32
0 32
0 32
0 32
0 32
0 32
0 32
0 32
15000000 32
0 32
0 32
0 32
0 32
0 32
0 32
0 32
0 32
0 32
0 32
0 32
0 32
0 32
0 32
根据 PEP,Windows'单调时钟的分辨率为 15mS。很可能是您在输出中看到的粒度。