0

This is specifically for Windows, I don't have this issue on linux based systems.

So, I have a program that creates subprocesses when running it. These subprocesses will terminate correctly if the program exits normally, or even with exceptions or ctrl+c event, by using try and KeyboardInterrupt and finally in if __name__ == '__main__':

However, if I kill the program in the middle, I'm talking about killing it in PyCharm, using the STOP button. Those subprocesses will not terminate. I'm not exactly sure what signal this STOP button sends on Windows.

I tried signal handling using signal.signal(signal.SIGTERM, handler). It doesn't work, I have tried SIGTERM, SIGINT, (SIGKILL, CTRL_C_EVENT, CTRL_BREAK_EVENT don't work in signal handler. ). None of them works. I have also read this post: How to handle the signal in python on windows machine

How can I gracefully exit in this scenario? This STOP button in PyCharm scenario.

Jack Feng
  • 895
  • 2
  • 9
  • 24
  • Have you tried to `Right-click` on the run-bar next to `Run` > `Close All`? – Sunny Patel Jan 14 '20 at 00:58
  • There's also using [`os.kill`](https://docs.python.org/3/library/os.html?highlight=os#os.kill)`(p.pid, SIGINT)` within your handler. – Sunny Patel Jan 14 '20 at 01:02
  • If the stop button terminates the process directly, there's nothing graceful about this. The best you can do is to add your process to a Job object that disallows breakaway, so all child processes will be in the job. Flag the job as kill-on-close. Keep the job handle open in the main program. Then if the main program gets terminated, the job will be closed, and at least all processes in the job are also terminated. – Eryk Sun Jan 14 '20 at 02:44

0 Answers0