Python:“牢不可破”无限循环已退出

2024-05-11 12:19:19 发布

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

我用一个RuuviTag蓝牙传感器记录数据,它把温度值发送给我的树莓。你知道吗

根据RuuviTag Python库docu,我应该使用函数RuuviTagSensor.get_datas(handle_data),它启动函数handle_data()的无限循环。 就我而言,我的设计是这样的:

def handle_data(found_data):
    temperature_measurement = found_data[1]['temperature']
    publish_via_mqtt(temperature_measurement)

我把一切都写在:

while True:
    try:
        RuuviTagSensor.get_datas(handle_data)
    except Exception:
        logger.exception(f"An error occurred at {datetime.now(timezone.utc)}")
        reconnect_mqtt()

然而,一夜之间,我打破了。。。你知道吗

日志上说:

INFO:ruuvitag_sensor.ble_communication:Problem with hciconfig reset. Retry reset.
INFO:ruuvitag_sensor.ble_communication:Problem with hciconfig reset. Retry reset.
INFO:ruuvitag_sensor.ble_communication:Problem with hciconfig reset. Retry reset.
INFO:ruuvitag_sensor.ble_communication:Problem with hciconfig reset. Exit.

所以看起来树莓帽有蓝牙问题,并试图重新连接到鲁维斯。。。当这三次都不起作用时,他就退出了PYTHON?我说的对吗?如果脚本退出,有没有办法重新启动整个脚本或者处理这个退出?你知道吗


Tags: infodatawithsensor树莓communicationresethandle
1条回答
网友
1楼 · 发布于 2024-05-11 12:19:19

在库code中,如果三次尝试后传感器无法启动,则调用exit(1)。你知道吗

调用exit将引发SystemExit异常。SystemExit不是从Exception继承的,因此不会被问题代码中的try/except块捕获。根据项目自述,这种行为似乎是故意的:

In case of errors, application tries to exit immediately, so it can be automatically restarted.

如果要强制库继续重试,可以在程序中捕获SystemExitexcept SystemExit:)。你知道吗

相关问题 更多 >