Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop python from propagating signals to subprocesses?

Tags:

I'm using python to manage some simulations. I build the parameters and run the program using:

pipe = open('/dev/null', 'w') pid = subprocess.Popen(shlex.split(command), stdout=pipe, stderr=pipe) 

My code handles different signal. Ctrl+C will stop the simulation, ask if I want to save, and exit gracefully. I have other signal handlers (to force data output for example).

What I want is to send a signal (SIGINT, Ctrl+C) to my python script which will ask the user which signal he wants to send to the program.

The only thing preventing the code to work is that it seems that whatever I do, Ctrl+C will be "forwarded" to the subprocess: the code will catch it to and exit:

try:   <wait for available slots> except KeyboardInterrupt:   print "KeyboardInterrupt catched! All simulations are paused. Please choose the signal to send:"   print "  0: SIGCONT (Continue simulation)"   print "  1: SIGINT  (Exit and save)"   [...]   answer = raw_input()   pid.send_signal(signal.SIGCONT)   if   (answer == "0"):     print "    --> Continuing simulation..."   elif (answer == "1"):     print "    --> Exit and save."     pid.send_signal(signal.SIGINT)     [...] 

So whatever I do, the program is receiving the SIGINT that I only want my python script to see. How can I do that???

I also tried:

signal.signal(signal.SIGINT, signal.SIG_IGN) pid = subprocess.Popen(shlex.split(command), stdout=pipe, stderr=pipe) signal.signal(signal.SIGINT, signal.SIG_DFL) 

to run the program but this gives the same result: the program catches the SIGINT.

Thanx!

like image 962
big_gie Avatar asked Sep 24 '10 22:09

big_gie


People also ask

How do you stop a subprocess run in Python?

To close a single subprocess in Python, use the kill() method. The kill() is a built-in method used for terminating a single subprocess. The kill() command keeps running in the background.

What are Python Subprocesses?

Subprocess in Python is a module used to run new codes and applications by creating new processes. It lets you start new applications right from the Python program you are currently writing. So, if you want to run external programs from a git repository or codes from C or C++ programs, you can use subprocess in Python.

How do you catch a signal in Python?

Python allows us to set up signal -handlers so when a particular signal arrives to our program we can have a behavior different from the default. For example when you run a program on the terminal and press Ctrl-C the default behavior is to quit the program.

How does Python signal work?

Python signal handlers are always executed in the main Python thread of the main interpreter, even if the signal was received in another thread. This means that signals can't be used as a means of inter-thread communication. You can use the synchronization primitives from the threading module instead.


1 Answers

Combining some of other answers that will do the trick - no signal sent to main app will be forwarded to the subprocess.

import os from subprocess import Popen  def preexec(): # Don't forward signals.     os.setpgrp()  Popen('whatever', preexec_fn = preexec) 
like image 129
Marek Sapota Avatar answered Oct 26 '22 07:10

Marek Sapota