gpt4 book ai didi

python - 当信号处理程序中遇到异常时,如何使 Django 信号处理程序不会静默失败?

转载 作者:IT老高 更新时间:2023-10-28 21:04:53 27 4
gpt4 key购买 nike

当处理程序中遇到异常时,如何使 Django 信号处理程序不会静默失败

在使用开发服务器时是否有记录所有这些错误的地方

为什么 django 信号处理程序无论如何都会默默地失败?是不是违反了Zen of Python中的一句话?

Zen of Python clearly states...

Errors should never pass silently.

这使它们成为调试的噩梦。你所看到的只是信号没有被触发......

我找到了 this问题,但答案对我来说没用,因为它对问题非常具体(答案建议使用 pyflakes,我已经使用 pydev 可以进行令人满意的静态分析)

最佳答案

是的,错误永远不会失败

是的,我的想法和你一样:错误永远不应该默默地失败

内置信号不会静默失败

Django 的内置信号不会静默失败,因为它们使用 send()

只有 send_robust() 会忽略异常

来自 send_robust() 的文档

send_robust() catches all errors derived from Python’s Exception class, and ensures all receivers are notified of the signal. If an error occurs, the error instance is returned in the tuple pair for the receiver that raised the error.

结论

由于 django 不使用 send_robust(),请调查它被调用的位置。我猜它在您的源代码中,或者在第三方应用程序的代码中。

关于python - 当信号处理程序中遇到异常时,如何使 Django 信号处理程序不会静默失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14481225/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com