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?

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top