Python 冻结 运行 exe 文件

Python freezes running exe file

我正在做一个简单的 python gui,点击按钮会 运行 一个简单的命令:

os.system("C:/cygwin64/bin/bash.exe")

当我查看控制台时,它 运行 是正确的,但我的人死机了,没有响应。 如果我 运行 控制台中的命令没有 python 它完美地工作并且我启动了 cygwin 终端。

如果您知道什么是 cygwin,是否有更好的方法在同一个终端启动它?

os.system 阻塞当前线程,您可以使用 os.popen 以便在另一个线程中执行此操作,并且它还为您提供了一些方法来 detach/read/write 等'该进程。 例如,

import os
a = os.popen("python -c 'while True: print(1)'")

将创建一个新进程,该进程将在您终止脚本后立即终止。 你可以做到

for i in a:
    print(i)
例如

,它会像 os.system 那样阻塞线程。 您可以 a.detach() 随时终止进程。

但是,os.system

import os
os.system("python -c 'while True: print(1)'")

它将永远输出 1,直到您终止脚本。

您可以使用程序包 subprocess 中的函数 Popen。它有许多可能的参数,允许您将输入管道输入到 and/or 管道输出,您是 运行 的程序。但是如果你只想执行bash.exe,同时允许你原来的Python程序继续运行并最终等待bash.exe完成,那么:

import subprocess

# pass a list of command-line arguments:
p = subprocess.Popen(["C:/cygwin64/bin/bash.exe"])

... # continue executing

# wait for the subprocess (bash.exe) to end:
exit_code = p.wait()