Python冻结运行exe文件

时间:2021-06-30 10:04:03

标签: python cygwin

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

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

当我查看控制台时,它运行正常,但我的家伙死机并且没有响应。 如果我在没有 python 的情况下在控制台中运行命令,它会完美运行,然后我会启动 cygwin 终端。

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

2 个答案:

答案 0 :(得分:1)

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

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)'")

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

答案 1 :(得分:1)

您可以在包 Popen 中使用函数 subprocess。它有许多可能的参数,允许您通过管道将输入和/或从您正在运行的程序中输出。但是,如果您只想执行 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()