Python windows script subprocess continues to output after script ends

Hi I am writing a python script in windows and using subprocess

I have a line like

results=subprocess.Popen(['xyz.exe'],stdout=subprocess.PIPE)

After the script ends, and I get back to the promp carrot in cmd, I see more output from the script being printed out. I'm seeing stuff like

Could Not Find xxx_echo.txt

Being printed out repeatedly.

How do I properly close the subprocess in windows?


Could Not Find xxx_echo.txt looks like an error message, which would likely be printed on stderr. Your call to Popen() does not collect the child's stderr output, so it will be printed in your terminal.

If your script does not wait for the child to complete, the child may still be executing after your script has exited. You should call wait() to wait for the child to complete execution. If you don't want to wait, call terminate() to terminate the child process.

You should also collect the stderr if you don't want it dumped to the terminal.

child = subprocess.Popen(['xyz.exe'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
child.wait()
print child.returncode

您需要在退出主脚本之前调用results.kill()或results.terminate()(它们是Windows上的别名)以结束子进程。


You have to call the terminate() method:

results.terminate()

There is also kill() for stuck sub-processes.

链接地址: http://www.djcxy.com/p/77114.html

上一篇: Python,子进程,管道和选择

下一篇: Python脚本子进程在脚本结束后继续输出