Python 3.3 包含一个名为 faulthandler
的模块,如果发生段错误,它会显示有用的回溯信息。 (对于 Python 3.3 之前的版本,模块可以是 obtained from PyPI 。)
默认情况下不启用该模块。它是这样启用的:
import faulthandler
faulthandler.enable()
这个功能非常有用。默认情况下未启用它是否有任何特殊原因?它对性能有任何负面影响吗?
最佳答案
This feature is very useful. Is there any particular reason it isn't enabled by default? Does it have any negative effects on performance?
原因是faulthandler会记住stderr的文件描述符,通常是fd 2。问题是fd 2可能会变成别的东西,像socket,pipe,重要的文件等等,没有可靠的检测方式这种情况,因此在 Python 中默认不启用故障处理程序会更安全。
faulthandler 在几乎所有情况下都是安全的,除非 faulthandler 存储的文件描述符被替换。文档中还描述了问题: https://docs.python.org/dev/library/faulthandler.html#issue-with-file-descriptors
注意:我写的是faulthandler。
关于python:使用故障处理程序有缺点吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21733856/