从Paho MQTT发送到云时缺少一些数据

2024-06-16 12:40:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将大约500个标签的数据(每秒更改数据)从Paho MQTT发送到Cloud。最初接收到所有数据,但一段时间后开始丢失样本

  • 试图改变“保持活力时间”
  • 检查了Wireshark监控流量

我的部分代码

def queue_consumer(queue):
    global TestCount

    global responseList
    while True:
        TestCount=0
        for items in range(0, queue.qsize()):
            responseList.append(queue.get_nowait())
            queue.task_done()
        item1="topicName"
        if item1 is None:
            print("BREAKKK")
            break
        else:
            mqttmessage1 = json.dumps(responseList)
            objClient = ConnectionClient()
            objClient.client.publish("topicName", mqttmessage1 , qos=1)
            responseList=[]
            del objClient
        time.sleep(1)           # time after which the next items in queue
                                # would be processed for publishing

我希望所有的数据都以时间戳的形式发布到云上


Tags: 数据infortimequeue时间items标签
1条回答
网友
1楼 · 发布于 2024-06-16 12:40:47

不清楚您的ConnectionClient()是否每次围绕主循环打开和关闭一个连接。但是,看起来每次都要关闭连接(通过del objClient)。如果是这样,我建议您在启动时创建一个mqtt.Client,并连接到服务。然后每次运行主循环时,都会发送消息,在程序退出之前不要关闭连接

您还可以检查服务提供者是否没有应用某些限制。每秒钟500个话题似乎相当多,除非你为服务付费

相关问题 更多 >