Python:为什么从线程调用“sys.exit(msg)”不将“msg”打印到stderr?

发布于 2024-07-29 22:32:01 字数 970 浏览 5 评论 0原文

今天我反对这样一个事实:从子线程调用 sys.exit() 不会杀死主进程。 我以前不知道这一点,这没关系,但我需要很长时间才能意识到这一点。 如果 sys.exit(msg)msg 打印到 stderr,那么它会节省很多时间。 但事实并非如此。

事实证明,这并不是我的应用程序中真正的错误; 它以一种自愿的方式调用了 sys.exit(msg) 并产生了有意义的错误——但我就是看不到这一点。

sys.exit() 的文档中指出“[...]任何其他对象都会打印到 sys.stderr 并导致退出代码为 1”

对于以下情况,这不正确:来自子线程的调用,其中 sys.exit() 显然表现为 thread.exit()“引发 SystemExit 异常。如果没有捕获,这将导致线程静默退出”

我认为当程序员想要 sys.exit(msg) 来打印错误消息,那么应该只打印它——与调用它的位置无关。 为什么不? 目前我看不出有什么理由。 至少在 sys.exit() 的文档中应该有一个提示,该消息不是从线程打印的。

你怎么认为? 为什么错误消息对线程隐藏? 这有道理吗?

谨致问候,

扬·菲利普·格尔克

Today I ran against the fact, that sys.exit() called from a child-thread does not kill the main process. I did not know this before, and this is okay, but I needed long time to realize this. It would have saved much much time, if sys.exit(msg) would have printed msg to stderr. But it did not.

It turned out that it wasn't a real bug in my application; it called sys.exit(msg) with a meaningful error in a volitional way -- but I just could not see this.

In the docs for sys.exit() it is stated:
"[...] any other object is printed to sys.stderr and results in an exit code of 1"

This is not true for a call from a child-thread, where sys.exit() obviously behaves as thread.exit():
"Raise the SystemExit exception. When not caught, this will cause the thread to exit silently"

I think when a programmer wants sys.exit(msg) to print an error message, then this should just be printed -- independent of the place from where it is called. Why not? I currently don't see any reason. At least there should be a hint in the docs for sys.exit() that the message is not printed from threads.

What do you think? Why are error messages concealed from threads? Does this make sense?

Best regards,

Jan-Philip Gehrcke

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

¢蛋碎的人ぎ生 2024-08-05 22:32:01

我同意 Python 文档关于 sys.exit 和 SystemExit 当由主线程以外的线程调用/引发时是不正确的,或者更准确地说是不完整的; 请在 Python 在线跟踪器上打开一个文档问题,以便可以在文档的未来迭代中解决这个问题(可能是不久的将来——文档修复比代码修复更容易、更顺利;-)。

当然,补救方法非常简单——只需将用作 threading.Thread 目标的任何函数与执行 try/的装饰器包装起来即可> except SystemExit, e: 围绕它,并在终止之前执行您需要的“写入 stderr”额外功能(或者,也许更好,使用logging.error 调用代替)。 但是,对于您正确指出的文档问题,很难考虑这样做,除非并且直到人们遇到了问题并且实际上必须花费一些时间进行调试才能确定它,因为您必须这样做(代表核心 Python 开发人员的集体——抱歉!)。

I agree that the Python docs are incorrect, or maybe more precisely incomplete, regarding sys.exit and SystemExit when called/raised by threads other than the main one; please open a doc issue on the Python online tracker so this can be addressed in a future iteration of the docs (probably a near-future one -- doc fixes are easier and smoother than code fixes;-).

The remedy is pretty easy, of course -- just wrap any function you're using as the target of a threading.Thread with a decorator that does a try/except SystemExit, e: around it, and performs the "write to stderr" extra functionality you require (or, maybe better, uses a logging.error call instead) before terminating. But, with the doc issue that you correctly point out, it's hard to think about doing that unless and until one has met with the problem and in fact has had to spend some time in debugging to pin it down, as you've had to do (on the collective behalf of the core python developers -- sorry!).

清眉祭 2024-08-05 22:32:01

python 中并非所有线程都是平等的。 从线程中调用 sys.exit 并不真正退出系统。 因此,从子线程调用 sys.exit() 是无意义的,因此它的行为与您期望的方式不同是有道理的。

这个页面更多地讨论了线程对象,以及线程和特殊的“main”之间的区别。 “ 线。

Not all threads in python are equal. Calling sys.exit from a thread, does not actually exit the system. Thus, calling sys.exit() from a child thread is nonsensical, so it makes sense that it does not behave the way you expect.

This page talks more about threading objects, and the differences between threads and the special "main" thread.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文