使用Paho MQTT在Python中订阅MQTT时出现线程问题 [英] Thread issue while subscribing to MQTT in Python using Paho MQTT

查看:824
本文介绍了使用Paho MQTT在Python中订阅MQTT时出现线程问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个python程序,它侦听MQTT主题并需要处理消息.我从命令行指定了许多参数,以不同方式评估消息.

I have a python program which listens to an MQTT topic and needs to process the message. I specify a number of arguments from the command line to evaluate the message differently.

import argparse
import datetime
import json

import paho.mqtt.client as mqtt

### MQTT Functions
def on_connect(mqttc, obj, flags, rc):
    print("Connected! - " + str(rc))

def on_message(mqttc, obj, msg):
    print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload))

def on_publish(mqttc, obj, mid):
    print("Published! "+str(mid))

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

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

if __name__ == "__main__":
    # Handle args
    parser = argparse.ArgumentParser(
        description='This is to be usedin conjunction with the WifiScanner on a Raspberry Pi')
    parser.add_argument('--topic', metavar='base/sub', type=str, nargs='?', help='Full topic to listen to. (Example "proximity/sensor")', default="proximity/#")
    parser.add_argument('--host', metavar='url', type=str, nargs='?',
                        help='UQL of MQTT server.')
    parser.add_argument('--graph', metavar='True/False', type=bool, nargs='?', help='Whether to print the data.', default=True)
    parser.add_argument('--timeout', metavar='sec', type=int, nargs='?', help='How long the device will be remembered', default=10)
    args = parser.parse_args()
    # MQTT
    mqttc = mqtt.Client()
    # mqttc.on_message = on_message
    mqttc.on_connect = on_connect
    mqttc.on_publish = on_publish
    mqttc.on_subscribe = on_subscribe
    # Uncomment to enable debug messages
    #mqttc.on_log = on_log
    mqttc.connect(args.host, 1883, 60)
    mqttc.subscribe(args.topic, 0)
    # Start to listen    
    while True:
        print mqttc.loop()

这个问题是,我看不到将命令行参数传递给on_message回调的简单方法.因此,我尝试使用.loop的返回值.但是,当我尝试使用Ctrl + Z退出(仅适用于键盘中断)时,它不会退出MQTT线程并使它们继续运行.

The problem with this, is that I can't see an easy way to pass the command line arguments to the on_message callback. So I tried using the return value of .loop. However, when I try to exit using Ctrl+Z (only keyboard interrupt that works), it does not exit the MQTT threads and leaves them running.

文档和示例都没有有关如何处理on_message回调之外的消息以及如何干净退出的示例.

The documentation and examples don't have an example on how to handle messages outside the on_message callback and how to cleanly exit.

因此,非常感谢您解决此问题.

So any help fixing this issue would be very appreciated.

预先感谢

推荐答案

您可以使用Client()构造函数中的userdata自变量.它最终被传递给每个回调.

You could use the userdata argument from the Client() constructor. It ends up being passed to every callback.

这篇关于使用Paho MQTT在Python中订阅MQTT时出现线程问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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