簡體   English   中英

python websocket-client,僅在隊列中接收最新消息

[英]python websocket-client, only receive most recent message in queue

我正在樹莓派上運行websocket-client,以使用跳躍運動websocket服務器發送的數據來控制伺服。 通過跳躍運動通過websocket連接發送的數據量是如此之大,以至於pi趕上最新消息會有很長的延遲,而延遲越長,它運行的時間就越長。

如何檢查隊列中的所有舊消息,而在檢查時僅使用最新消息? 目前,我正在這樣做:

ws = create_connection("ws://192.168.1.5:6437")
    while True:
        result = ws.recv()
        resultj = json.loads(result)
        if "hands" in resultj and len(resultj["hands"]) > 0:
                # print resultj["hands"][0]["palmNormal"]
                rotation = math.atan2(resultj["hands"][0]["palmNormal"][0], -resultj["hands"][0]["palmNormal"][1])
                dc = translate(rotation, -1, 1, 5, 20)
                pwm.ChangeDutyCycle(float(dc))

由於Leap服務以大約每秒110幀的速度發送數據,因此您有大約9ms的時間進行任何處理。 如果超出此范圍,那么您將落后。 最簡單的解決方案可能是創建人為的應用幀速率。 對於每個循環,如果您還沒有到達下一次更新的時間,則只需獲取下一條消息並將其丟棄。

ws = create_connection("ws://192.168.1.5:6437")
allowedProcessingTime = .1 #seconds
timeStep = 0 
while True:
    result = ws.recv()
    if time.clock() - timeStep > allowedProcessingTime:
        timeStep = time.clock()
        resultj = json.loads(result)
        if "hands" in resultj and len(resultj["hands"]) > 0:
                # print resultj["hands"][0]["palmNormal"]
                rotation = math.atan2(resultj["hands"][0]["palmNormal"][0], -resultj["hands"][0]["palmNormal"][1])
                dc = translate(rotation, -1, 1, 5, 20)
                pwm.ChangeDutyCycle(float(dc))
        print "processing time = " + str(time.clock() - timeStep) #informational

(我尚未對您的代碼運行此修改,因此適用通常的警告)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM