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

时间:2018-10-14 17:01:34

标签: python error-handling

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

例如,如果我尝试打印未定义的对象,例如threadObj(请参见第27行),则脚本不会走得更远,甚至都不会在同一行上打印字符串

这是预期的行为吗? 该问题在Windows 10和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()

0 个答案:

没有答案