0

On a Windows 7 machine:-

  • I have a main (Python) program that I start on a command prompt [main process].

  • This program spawns a child (Python) program [child process].

  • I close the command prompt.

Result:-

  • Child process ends immediately.

On the other hand if I end the main program from task manager, I observe that the child process is still running.

I was wondering why the 2 approaches do not have the same results? Is it sending some different signal in the two cases?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Barun Sharma
  • 1,452
  • 2
  • 15
  • 20
  • Which platform are you working on? If it's POSIX, when a controlling process exits, the SIGHUP (hangup) is sent to all its child processes (which may or may not actually terminate them). Presumably, in the 'task manager' environment, the parent process is not made into a controlling process. See [Consequences of Process Termination](http://pubs.opengroup.org/onlinepubs/9699919799/functions/_exit.html) and related information. – Jonathan Leffler Jan 30 '14 at 06:48
  • AFAIK when you close a terminal it closes all processes attached to it. You should detach the child process from the terminal (e.g. daemonizing it). – Bakuriu Jan 30 '14 at 08:35
  • @JonathanLeffler I am using windows(win7) OS. Went through your referenced doc, it deals majorly with POSIX signal handling. – Barun Sharma Jan 30 '14 at 09:22
  • @Bakuriu Dont find any way to do the same in windows. The only thing I can do is make a service for the child process. But I want to avoid that stuff. Is there still something that helps? :( – Barun Sharma Jan 30 '14 at 09:27
  • 1
    That's why it is important to identify where you are working. The rules are radically different, and what applies to Unix (POSIX, Linux) does not necessarily apply to Windows, and vice versa. – Jonathan Leffler Jan 30 '14 at 13:29
  • 1
    The child process is assigned to the command window when it is launched. The Windows API allows you to specify that the child process get a new window, or no window. Hopefully, Python provides you with the same options when launching new processes. (The Windows API also allows a program to change command windows after being launched, but I have no idea whether Python exposes that to the programmer or not.) – Harry Johnston Jan 30 '14 at 23:16
  • Thanks @HarryJohnston. Your suggestion helped a lot. Should have figured that myself. Yes Python provides the same. It solved my problem. Will post my solution. – Barun Sharma Feb 01 '14 at 19:16

1 Answers1

1

Comments to the question pointed me to get the answer.

I was using subprocess.Popen(args) to spawn the child process. This would spawn the child process successfully but the child process would be launched in the same command window as its parent.

Going through subprocess Popen doc, I found some additional arguments to be passed in order to launch child process in another command window.

Launching the child with the following arguments solved my problem.

subprocess.Popen(args, shell=True, creationflags=subprocess.CREATE_NEW_CONSOLE)

The last argument subprocess.CREATE_NEW_CONSOLE is only for windows.

Barun Sharma
  • 1,452
  • 2
  • 15
  • 20