简体   繁体   English

Python - Stray cmd.exe窗口导致程序挂起。 我如何关闭这个迷路程序?

[英]Python - Stray cmd.exe window causes program to hang. How can I close this stray program?

I have a Python file which starts a new process via Popen. 我有一个Python文件,它通过Popen启动一个新进程。 The Python codes should wait until this new process is complete before resuming it's code. Python代码应该等到这个新进程完成后才能恢复它的代码。 However, the process Python starts also spawns another cmd.exe window which does some work, but stays open after it's done. 但是,Python启动的进程还会生成另一个cmd.exe窗口,该窗口可以完成一些工作,但在完成后会保持打开状态。 The process Python starts does close after it's done, but the stay cmd.exe window remains open causing my Python program not to continue. Python启动的过程在完成后关闭,但是停留cmd.exe窗口仍然打开,导致我的Python程序无法继续。 What are the best ways to close this stay window? 关闭此停留窗口的最佳方法是什么?

Here's the call I'm making to popen: 这是我正在打电话的电话:

p = Popen(command, stdout=PIPE, stderr=STDOUT, stdin=PIPE)

command is running another python file. 命令正在运行另一个python文件。 I set it equal to p because I later communicate with the new process via grep_stdout. 我将它设置为等于p,因为我稍后通过grep_stdout与新进程通信。

This can be solved by using pythonw.exe instead of python.exe to execute your second script. 这可以通过使用pythonw.exe而不是python.exe来执行第二个脚本来解决。 If you're running the script directly (rather than invoking the interpreter yourself), you should rename it with a .pyw file extension, as that should be associated with pythonw.exe by default. 如果您直接运行脚本(而不是自己调用解释器),则应使用.pyw文件扩展名重命名,因为默认情况下应与pythonw.exe关联。

See this section of the Python docs for more information about executing Python files on Windows. 有关在Windows上执行Python文件的更多信息,请参阅Python文档的此部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM