【发布时间】:2023-04-02 15:20:01
【问题描述】:
我的代码中有一个地方,我在 dict 的键名中犯了一个错误。花了一些时间才明白为什么代码没有跑过那个地方因为没有抛出回溯。
代码如下,为了完整起见,我把它放在→→→突出问题的地方:
class Alert:
lock = threading.Lock()
sent_alerts = {}
@staticmethod
def start_alert_listener():
# load existing alerts to keep persistancy
try:
with open("sent_alerts.json") as f:
json.load(f)
except FileNotFoundError:
# there is no file, never mind - it will be created at some point
pass
# start the listener
log.info("starting alert listener")
client = paho.mqtt.client.Client()
client.on_connect = Alert.mqtt_connection_alert
client.on_message = Alert.alert
client.connect("mqtt.XXXX", 1883, 60)
client.loop_forever()
@staticmethod
def mqtt_connection_alert(client, userdata, flags, rc):
if rc:
log.critical(f"error connecting to MQTT: {rc}")
sys.exit()
topic = "monitor/+/state"
client.subscribe(topic)
log.info(f"subscribed alert to {topic}")
@staticmethod
def alert(client, userdata, msg):
event = json.loads(msg.payload)
log.debug(f"received alert: {event}")
→→→ if event['ok']:
# remove existing sent flag, not thread safe!
with Alert.lock:
Alert.sent_alerts.pop(msg['id'], None)
return
(...)
来自上一行的日志是
2021-01-14 22:03:02,617 [monitor] DEBUG received alert: {'full_target_name': 'ThisAlwaysFails → a failure', 'isOK': False, 'why': 'explicit fail', 'extra': None, 'id': '6507a61c9688199a34cb006b354c8433', 'last': '2021-01-14T22:03:02.612912+01:00', 'last_ko': '2021-01-14T22:03:02.612912+01:00'}
这是我试图错误访问ok 的dict,它应该引发异常和回溯。但什么也没有发生。代码并没有更进一步,就好像错误被无声地丢弃(并且该方法无声地失败)。
我试图在 log.debug() 和 if 之间放置一个 raise Exception("wazaa") - 同样的,该方法在此时失败,但没有引发异常。
我不知道为什么通过回溯无法看到异常?
alert() 方法在单独的线程中调用,如果这很重要的话。为了完整起见,我尝试了以下代码以确保 threading 不会干扰但不会(我看不出它应该这样做的原因)
import threading
class Hello:
@staticmethod
def a():
raise Exception("I am an exception")
threading.Thread(target=Hello.a).start()
输出
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Python38\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "C:\Python38\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:/Users/yop/AppData/Roaming/JetBrains/PyCharm2020.3/scratches/scratch_1.py", line 7, in a
raise Exception("I am an exception")
Exception: I am an exception
【问题讨论】:
-
好像错误被默默丢弃了,你可以放一个print看看是不是这样。
-
@Countour-Integral:我不明白。不是日志没有显示,而是 Traceback
-
我不知道
paho是什么,但我猜它是在try内部执行client.on_message。client对象可能包含捕获的错误列表? -
@WoJ 是的,但是
if event['ok']会发生什么,我说你可以在 if 里面放一个print,在外面放一个,看看函数是否被“杀死”。你也可以尝试raiseing 你自己的异常,看看会发生什么。 -
调用
alert的东西可能会捕获并抑制异常。由于我们不知道这个on_message是什么,也不知道它是如何工作的,所以很难说。如果event中没有“ok”键,则会引发异常,并且可能有什么东西正在捕获它。
标签: python python-3.x exception