python脚本停止运行,但未显示错误

2024-06-06 08:39:59 发布

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

使用下面的python脚本,但我发现很难检查它是否运行良好,因为我注意到对于某些错误,控制台没有显示任何错误

例如,如果我试图打印一个未定义的对象,比如threadObj(参见第27行),脚本就不会更进一步,甚至不会在同一行打印字符串

这是预期的行为吗? 这个问题在Windows10和linux上都会发生(raspbian)

import json
from math import ceil
import os
import queue
import sys
import subprocess
import time

import paho.mqtt.client as mqtt


def on_connect(client, userdata, flags, rc):
    print("Connected to {0} with result code {1}".format(HOST, rc))
    # Subscribe to any hotword topic --- old code:   # client.subscribe("hermes/hotword/default/detected")
    client.subscribe("hermes/hotword/#")

    # Subscribe to any topic starting with 'hermes/intent/'
    client.subscribe('hermes/intent/#')



def on_message(client, userdata, msg):

    print("- Message received on topic {0}: {1}".format(msg.topic, msg.payload))
    print('* ledPulse instance count {0}'.format(threadObj))

    if msg.topic == 'hermes/hotword/default/detected':
        print("Wakeword detected! Wakeword light!")

    if msg.topic == 'hermes/intent/mywai:ShowMarketplace':
        # intent ShowMarketplace light
        print("Intent detected! ShowMarketplace light!")

    if msg.topic == 'hermes/intent/mywai:Shutdown':
        print("Intent detected! Shutdowndevice")
        os.system('shutdown -s')   #windows
        os.system('sudo shutdown now')  #linux



#########################################################################
# MAIN
#########################################################################

if __name__ == '__main__':
    #main()

    HOST = '192.168.1.6'
    PORT = 1883


    client = mqtt.Client()
    client.on_connect = on_connect
    client.on_message = on_message

    client.connect(HOST, PORT, 60)
    client.loop_forever()

Tags: toimportclienttopicifosonconnect