我使用套接字在几秒钟内收到约20个json,我必须处理Json中的一个特定数据并触发(如果此数据超过1)一个事件。这里的问题是所有接收到的数据都被一一处理,这会导致巨大的延迟。
import time
import socket
import json
from subprocess import call
command1 = "omxplayer /media/dd/xXx/clap.wav"
SENSOR_ID='B35'
s = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
s.bind("\0xXx.data")
print ("Connected...")
while True:
data = s.recv(1024)
j=json.loads(data [0:(len(data)-1)]) #trunking the last character of dat$
if SENSOR_ID in j: #filter on the SENSOR_ID
sensorData = j[SENSOR_ID] #creating a new dict called sensorData
if sensorData[u'***'][0] >= 1:
call(command1, shell=True)
time.sleep(0.5)
s.close()
这是收到的json:
{"B35":{
"Time":1533208501.720,
"PacketID":2358,
"Loss":0,
"RSSI":-57,
"***":[50.4,49.8,49.8,51,51.7,49.1,49.8,49.8,50.1,48.5],
"****":[-10.1,-9.8,-9.8,-7.9,-11.4,-9.2,-9.8,-10.1,-9.5,-9.2],
"*****":[-23.6,-21.2,-21.3,-20.9,-20.5,-22.4,-23.8,-23.4,-21.5,-23.9]
}}
这是一个快速接收数据的脚本,它会导致巨大的延迟!我已经读过Yield关键字,这是否可以帮助我解决问题?