如何最大程度地减少大量消息延迟

时间:2020-05-15 06:53:49

标签: python-3.x mqtt delay message paho

我有七个树莓派作为客户端a,一个树莓派作为客户端b,它们通过mqtt在彼此之间传递消息。到目前为止,它运作良好,但有时会延迟2秒以上。在大多数情况下,延迟约为0,02-0,2s,但有时会有一些延迟峰值,且延迟超过2s。 我认为,一旦客户端b同时从a客户端收到太多消息,它就会出现。 你们中有人知道这是什么原因以及如何解决这个问题吗? 谢谢,保持健康!

调用run()时,客户端a发布到客户端b

客户a

from datetime import datetime
import time
import paho.mqtt.client as mqtt


class CarClient:

    def __init__(self, id):
        self.client = mqtt.Client(id)
        self.client.connect("192.168.178.41", 1883, 60)  # connect to broker
        self.newSpeed = 0
        self.holdSpeedTime = 5
        self.speedDuration = 0
        self.id = id
        self.time_sent = 0

    def on_connect(self, mqttc, obj, flags, rc):
        print("connected" + str(rc))

    def on_message(self, mqttc, obj, msg):
        answerTuple = eval(msg.payload)
        self.newSpeed = int(answerTuple[0])
        self.holdSpeedTime = answerTuple[1]
        self.client.unsubscribe("test_channel")
        time_feedback = time.time()
        delay = time_feedback - self.time_sent
        print(str(delay))

    def on_disconnect(self, mqttc, flags, rc):
        print("disconnected" + str(rc))

    def on_publish(self, mqttc, obj, mid):
        print("Data published " + str(mid))
        print("published")

    def on_subscribe(self, mqttc, obj, mid, granted_qos):
        print("Subscribed: " + str(mid) + " " + str(granted_qos))

    def on_unsubscribe(self, mqttc, flags, rc):
        print("unsubscribed" + str(rc))

    def on_log(self, mqttc, obj, level, string):
        print(string)

    def run(self, carID, connectionPoint):
        self.time_sent = time.time()
        self.tuple = (carID, connectionPoint)

        # assining functions to callback
        self.client.on_disconnect = self.on_disconnect
        self.client.on_connect = self.on_connect
        self.client.on_message = self.on_message
        # self.client.on_subscribe = self.on_subscribe
        # self.client.on_unsubscribe = self.on_unsubscribe
        self.client.loop_start()
        self.client.subscribe(str(self.id), 0)  # subscribe topic

        self.client.publish("test_channel1", str(self.tuple))

        time.sleep(1)

        carFeedback = (self.newSpeed, self.holdSpeedTime)
        self.newSpeed = 0
        self.holdSpeedTime = 5

        return carFeedback

客户端b

import paho.mqtt.client as mqtt
import time

crossingList = []



def on_subscribe(client, userdata, mid, granted_qos):  # create function for callback
    print("subscribed with qos", granted_qos, "\n")


def on_disconnect(client, userdata, rc=0):
    print("disconnected")


def on_connect(client, userdata, flags, rc):
    print("connected")


def on_message(client, userdata, message):
    dataTuple = eval(message.payload)  # message is a tuple of (carID, connectionPoint)
    print(str(dataTuple))
    addToList(dataTuple)


def on_publish(client, userdata, mid):
    print("published")


def addToList(dataTuple):
    carID = dataTuple[0]
    connectionPoint = dataTuple[1]
    crossingList.append((carID, connectionPoint))


def logic():
    carID = crossingList.__getitem__(0)[0]
    connectionPoint = crossingList.__getitem__(0)[1]

    if connectionPoint == 600:

        newSpeed = 400
        holdTime = 5
        client.publish( carID, str((newSpeed, holdTime)))
        del crossingList[0]

    else:
        newSpeed = 500
        holdTime = 3
        client.publish(carID, str((newSpeed, holdTime)))
        del crossingList[0]



client = mqtt.Client("Logic")  # create client object

client.on_subscribe = on_subscribe  # assign function to callback
client.on_disconnect = on_disconnect  # assign function to callback
client.on_connect = on_connect  # assign function to callback
client.on_message = on_message
client.connect("192.168.178.41", 1883, 60)  # establish connection
time.sleep(0.1)
client.loop_start()
client.subscribe("test_channel1")
count = 1
while True:  # runs forever break with CTRL+C
    if len(crossingList) != 0:
        logic()
    else:
        count = count + 1

0 个答案:

没有答案