在python中,在不等待.exe完成的情况下并行运行一个可执行的.exe

在python中,在不等待.exe完成的情况下并行运行一个可执行的.exe,python,parallel-processing,subprocess,execution,Python,Parallel Processing,Subprocess,Execution,这只是一个例子,但可能会出现更多的情况。假设您有一个来自“myscript.py”文件的可执行文件,虽然为true,但它实际上是从文件中提取的条件 # myscript.py file to be executed.. bool_condition = True while bool_condition : bool_condition = pickle.load(open('file_condition.p','rb')) # -- script lines under w

这只是一个例子,但可能会出现更多的情况。假设您有一个来自“myscript.py”文件的可执行文件,虽然为true,但它实际上是从文件中提取的条件

# myscript.py file to be executed..

bool_condition = True
while bool_condition :
    bool_condition = pickle.load(open('file_condition.p','rb'))

    # -- script lines under while-condition -- #

# endwhile bool_condition 
原则上,它是无限的,除非另一个脚本更改文件_condition.p的内容。现在考虑一个GUI,可执行文件的激活来自按下一个按钮,并且可执行文件的停止也来自按钮操作,因此如果按下STOP按钮,条件P的内容就会改变,因此BooLyValk变量也从真到假,因此执行停止。

关键是当按下开始按钮时,有一行运行“myscript”的执行,而不等待该行的执行结束(因为否则,开始按钮将冻结,您无法按下停止按钮)。我知道如何从python文件“myscript.py”中执行无等待并行执行

但是,它不能从*.exe文件“myscript.exe”中工作

对我来说,不可能认为Python无法执行*.exe文件的无等待并行执行(即不等待执行结束,如os.sys(myscript.exe))。Python真的能做到吗?

试试:

pid = subprocess.Popen(["myscript.exe"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
将绝对路径传递给“myscript.exe”也会使整个调用更加安全


通过将
os.sys.executable
作为第一个参数传递给Popen,可以有效地调用python。如果您想启动python脚本,但exe文件不是python脚本,这是它无法工作的原因。

我确认它可以工作。谢谢,您的解决方案简单而优雅
# 1st try..

pid = subprocess.Popen([os.sys.executable, "myscript.exe"], 
      stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)

# 2nd try..

pid = subprocess.Popen(os.system(myscript.exe), stdout=subprocess.PIPE, 
      stderr=subprocess.PIPE, stdin=subprocess.PIPE)
pid = subprocess.Popen(["myscript.exe"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)