Python signal.signal is it preventing propagation? -


I have this code (partially taken from Python Docs):

 < Code> import signal def handler (signal, frame): print 'signal signal signal' signal, signal s = signal signal (signal. CGInt, handler) some_fancy_code () # This code is using subprocess. To call popin () another script, singal.signal (signal. Sigant, s)  

What have I got now that if I do Ctrl + C in my program then It properly enters that handler and print. Now, I thought that after receiving Ctrl + C, my handler would press the default handler, for example my subprances. Popin keyboard will not get interrogation signal. But this is not the case.

But when we replace 'handler' with 'signal' signals, this promotion never happens. Modified snippet:

  import signal s = signal.signal (signal.SIGINT, signal.SIG_IGN) some_fancy_code () # This code is using subprocess.open () singal another script. Signal (to call) signal sigant, s)  

Is it because SIG_IGN has any kind of 'magic' signal written in the language? Or is there a way to suppress the same in my own handler?

After reading a little question on stack overflow, I am a little confused if someone can explain to me why there is such a difference in practice

This is a specified POSIX behavior of signals:

  via fork (2) A child made is a copy of your parents' signal disposition. During execution (2), the behavior of the handle signal is reset by default; The nature of neglected signals has been left unchanged.  

When you execute your second script in the first case, the SIGINT handler is reset to the default handler in the second script (ending the default behavior process) - of course, a And the script can install its own handler and can change this behavior.

However, in the second case, you have configured SIGINT to ignore this behavior will be promoted in the second script, as shown in above definition. Then, another script can change this behavior by establishing its own handler.

Therefore its use is not directly with Python, it is expected behavior of the posical signal handling implementation of the underlying operating system.

PS If you are wondering whether fork () and execution (), then fork () creates a copy of the ongoing process (a child) and execve () changes the current process with the other The underlying mechanisms used by subprocess.Popen () to run another script are: first copy the current process and then replace it with the target process.


Comments