我正在用python编写GUI应用程序,并且使用了PyQt5和paho-mqtt。但是,似乎在调用函数Mqtt.connect()时会导致分段错误。如下面的简单示例所示,它确实会在我的计算机上崩溃,这是一个奇怪的问题,但我真的不知道为什么...
我的开发环境是python 3.7.1,PyQt5 5.12.1,paho-mqtt 1.4.0,ubuntu 18.04
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import QMainWindow , QApplication
import paho.mqtt.client as mqtt
# create a single window
app = QApplication(sys.argv)
win = QMainWindow()
win.setWindowTitle("mainWindowTitle")
win.resize(350, 250)
win.setObjectName("MainWindow")
#win.setStyleSheet("#MainWindow{border-image:url(./images/python.jpg);}")
#win.setStyleSheet("#MainWindow{background-color: yellow}")
win.show()
#----with this block, it crashs
HOST = "test.mosquitto.org"
PORT = 1883
client = mqtt.Client()
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
def on_message(client, userdata, msg):
print(msg.topic+" " + ":" + str(msg.payload))
#self.sinOut.emit(str(msg.payload))
form.thread.addCustomAd(msg.payload)
client.on_connect = on_connect #cause Segmentation fault here
client.on_message = on_message
client.connect( HOST, PORT, 60)
client.subscribe("someTopic", 0)
client.loop_start()
#------with above code block, it crashes ----------
sys.exit(app.exec_())