Dra*_*els 1 python signals interrupt
所以我有这个代码(部分取自python文档):
import signal
def handler(signum, frame):
print 'Signal handler called with signal', signum
s = signal.signal(signal.SIGINT, handler)
some_fancy_code() # this code is using subprocess.Popen() to call another script
singal.signal(signal.SIGINT, s)
Run Code Online (Sandbox Code Playgroud)
我现在发现,如果我在程序中执行 Ctrl+C,它会正确输入该处理程序并进行打印。现在,我的想法是,在收到 Ctrl+C 后,我的处理程序将抑制默认处理程序,因此例如我的 subprocess.Popen 将不会收到 KeyboardInterrupt 信号。但这种情况并非如此。
但是当我们用“signal.SIG_IGN”替换“handler”时,这种传播永远不会发生。修改后的片段:
import signal
s = signal.signal(signal.SIGINT, signal.SIG_IGN)
some_fancy_code() # this code is using subprocess.Popen() to call another script
singal.signal(signal.SIGINT, s)
Run Code Online (Sandbox Code Playgroud)
这是因为 SIG_IGN 是用语言本身编写的某种“神奇”信号吗?或者也许有一种方法可以在我自己的处理程序中进行类似的抑制?
在阅读了一些有关堆栈溢出的问题后,我有点困惑。如果有人能为我解释为什么行为如此不同。
这是信号的指定 POSIX 行为:
\n\n A child created via fork(2) inherits a copy of its parent\'s signal dis\xe2\x80\x90\n positions. During an execve(2), the dispositions of handled signals\n are reset to the default; the dispositions of ignored signals are left\n unchanged.\nRun Code Online (Sandbox Code Playgroud)\n\n当您在第一种情况下执行(fork/execve)另一个脚本时,SIGINT 处理程序将重置为另一个脚本中的默认处理程序(默认行为是终止进程) - 当然,另一个脚本可以安装自己的处理程序并改变这种行为。
\n\n但是,在第二种情况下,您已将 SIGINT 配置为被忽略。此行为将传播到另一个脚本,如上面的定义所示。同样,另一个脚本可以通过安装自己的处理程序来更改此行为。
\n\n所以这和Python没有直接关系。这是底层操作系统的 POSIX 信号处理实现的预期行为。
\n\n附言。如果您想知道 fork() 和 execve() 是什么,fork() 创建正在运行的进程(子进程)的副本,而 execve() 用另一个进程替换当前进程。这是 subprocess.Popen() 用于运行“另一个脚本”的底层机制:首先复制当前进程,然后将其替换为目标进程。
\n| 归档时间: |
|
| 查看次数: |
1647 次 |
| 最近记录: |