在 Python 信号处理器中调用 print 是否安全?
Is it safe to call print in a Python signal handler?

原始链接: https://iafisher.com/2026/08/sigprint

Python 的信号处理与 C 语言不同,因为它会将用户提供的函数推迟到解释器处于一致状态时执行,从而绕过了传统的底层限制。然而,Python 的信号处理程序本质上是可重入的,这意味着信号可能会中断处理程序自身。 一项涉及快速信号轰炸的压力测试表明,这种重入性可能会触发 `RuntimeError`——特别是在像 `print` 这样的函数向缓冲区写入内容时被中断的情况下。虽然这证明了极端条件可能导致程序崩溃,但这在实际应用中几乎不是问题。与 C 语言中不安全的信号处理可能导致静默损坏或死锁不同,Python 的这种行为会产生清晰的异常。尽管如此,不在信号处理程序中执行繁重的工作仍然是最佳实践。

抱歉。
相关文章

原文
“These systems collect and process.”

We learned earlier that because Python has two signal handlers, the onerous restrictions on what functions a signal handler may call do not apply to Python, because CPython does not call the user-supplied Python signal handler inside the low-level C signal handler, where those restrictions do apply, but arranges for it to be called later, when the interpreter is in a consistent state.

We also learned that Python signal handlers are unexpectedly reentrant – if a signal arrives while a Python signal handler is running, the signal handler can be called again in the middle of the first call.

What happens if a signal handler is reentered in the middle of a call to print? Let's stress-test it by sending ourselves a rapid barrage of signals:

import os, signal, subprocess

def sighandler(_signo, _frame):
    print("signal received")

signal.signal(signal.SIGUSR1, sighandler)
subprocess.run("for x in {1..50}; do kill -USR1 %s; done" % os.getpid(), shell=True)

Running this program on my machine produced:

  File "multiple_signals.py", line 6, in sighandler
    print("signal received")
  File "multiple_signals.py", line 6, in sighandler
    print("signal received")
  File "multiple_signals.py", line 6, in sighandler
    print("signal received")
  [Previous line repeated 2 more times]
RuntimeError: reentrant call inside <_io.BufferedWriter name='<stdout>'>

The test program shows that under extreme circumstances, calling print in a signal handler may cause your program to crash. I want to emphasize that this requires extreme circumstances: it is unlikely that a real program would face these conditions, and even so, failing with an exception is more palatable than the possible consequences of unsafe signal handlers in C, which include deadlock, corrupted data structures, and silent failures. So I view this as another bit of signals trivia and not a practical consideration for writing signal handlers – though I still advise against doing non-trivial work in a signal handler.

联系我们 contact @ memedata.com