I have a server that starts a subprocess, and I can manage to do a send_signal(SIGTERM)
which will kill the process. But not gracefully.
If I call my subprocess from shell (i.e. as a single process), the defined signal handler will kick in and exit gracefully.
server.py:
(so.. from another script I first call start_app()
, and later exit_app()
def start_app():
app = subprocess.Popen("python app.py")
def exit_app():
p = app.poll()
if p==None:
print("Subprocess is alive") # debug
app.send_signal(signal.SIGTERM)
app.py
def exit_signal_handler(signal, frame):
print("Terminate signal received")
app.exit()
if __name__ == '__main__':
app = QApplication(sys.argv)
signal.signal(signal.SIGTERM, exit_signal_handler)
signal.signal(signal.SIGINT, exit_signal_handler)
sys.exit(app.exec())
Again, if I call app.py from shell and send a SIGTERM
signal I get a trace Terminate signal received
and app closes.
But when app.py is started by server and I call exit_app
in server, I get a trace Subprocess is alive
(from server.py) and app is killed but the signal is not caught in app's signalhandler exit_signal_handler
EDIT:
It seems send_signal()
doesn't send a signal to the subprocess in the sense that subprocess catches the signal. It send`s a signal for an action to take place on the subprocess:
def send_signal(self, sig):
"""Send a signal to the process
"""
if sig == signal.SIGTERM:
self.terminate()
elif sig == signal.CTRL_C_EVENT:
os.kill(self.pid, signal.CTRL_C_EVENT)
elif sig == signal.CTRL_BREAK_EVENT:
os.kill(self.pid, signal.CTRL_BREAK_EVENT)
else:
raise ValueError("Unsupported signal: {}".format(sig))
This probably answers my question but I'll leave it open...