Python中实时接收UDP数据包时的延迟 [英] Delay when receiving UDP packets in Python in real-time

查看:41
本文介绍了Python中实时接收UDP数据包时的延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Opentrack 来跟踪头部运动,坐标通过 UDP 发送到我的 Python 程序.该程序的工作原理是它正确接收坐标,但是我注意到信息到达之前有很大的延迟.

I am using Opentrack to track head movements and the coordinates are sent through UDP to my Python program. The program works in the sense that it receives the coordinates correctly, but however I have noticed that there is a large delay before information arrives.

在观察行为后,在我看来,跟踪软件将坐标发送到我的程序从中获取数据的某个缓冲区,但我的程序获取数据的速度比缓冲区填满的速度慢.这意味着,如果我移动我的头,那么这些新坐标都已被检测到,但程序必须逐渐通过导致延迟的缓冲区.这是一个问题,因为我将它用作需要始终将当前坐标立即发送到我的程序的实时应用程序.

After observing the behaviour it seems to me that the tracking software sends the coordinates to some buffer that my program fetches the data from, but my program is slower to fetch the data than the speed which the buffer fills up. This means that if I move my head then those new coordinates have all been detected but the program has to gradually go through the buffer which causes the delay. This is a problem since I am using this as a real-time application that needs to send the current coordinates instantly to my program all the time.

我不确定问题是否出在 Opentrack 软件中,我是否应该前往该社区寻求帮助,或者我是否可以在 Python 中修复它...

I'm not sure if the problem is in the Opentrack software and if I should head over to that community for help, or if I can fix it in Python...

基本上,我只是希望没有缓冲区,而是它只是发送当前坐标(如果我的应用程序中丢失了某些测量坐标,则无关紧要).

Basically, I just wish there wasn't a buffer but that it instead just sent the current coordinates (it doesn't matter if some measured coordinates are lost in my application).

def connect(self, PORT):
    HOST = ''   # Symbolic name meaning all available interfaces
    #PORT = 8888 # Arbitrary non-privileged port

    # Datagram (udp) socket
    try :
        self.s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        print 'Socket created'
    except socket.error, msg :
        print 'Failed to create socket. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
        sys.exit()


    # Bind socket to local host and port
    try:
        self.s.bind((HOST, PORT))
    except socket.error , msg:
        print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
        sys.exit()

    print 'Socket bind complete'

#now keep talking with the client
def fetch(self):
    # receive data from client (data, addr)
    d = self.s.recvfrom(1024)
    data = d[0]
    addr = d[1]

    if data: 

        reply = 'OK...' + data

        self.s.sendto(reply , addr)

        unpacked_data = struct.unpack('dddddd', data)
        x = unpacked_data[0]
        y = unpacked_data[1]
        z = unpacked_data[2]
        return (x, y, z)

推荐答案

所以我加了一行解决了

self.s.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 1)

在我的代码中.这将缓冲区设置为大小 1 解决了我的问题.

in my code. This sets the buffer to size 1 which solved my problem.

这篇关于Python中实时接收UDP数据包时的延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆