如何在脚本中打开程序并继续运行脚本



我一直在尝试使用os.system自动打开Xampp,然后运行我用tkinter制作的脚本。但问题是脚本会停止,直到我手动关闭子流程。

这里是一个例子。

import os
direccion = 'C:/xampp/xampp-control.exe'
os.system(direccion)
print("Hello world")

print((不工作,当我手动关闭examplep时才开始运行。在打开xampp的同时,我能做些什么来继续运行脚本?

在python代码中逐行执行。。。

有两种方法:

首先也是最好的方法是将代码包装成一个函数。。。

例如

def opener(direccion: str):
os.system(direccion)

然后随时随地使用opener function!(例如,在打印报表之后或之后(

第二种方法是使用threading模块。。。

例如

import os
import threading
import concurrent.futures
def opener(direccion: str):
os.system(direccion)
def printer():
print("Hello World!")
with concurrent.futures.ThreadPoolExecutor() as executor:
f1 = executor.submit(opener, "C:/xampp/xampp-control.exe")
f2 = executor.submit(printer)
print(f1.result())
print(f2.result())

关于线程的更多信息&concurrent.futures模块

我找到了一个适合我的解决方案:

import os
import subprocess
def SomeFunction():
subprocess.Popen([myAppWithPath])
time.sleep(5)
os.system("taskkill /f /im MyApp.exe")
...
# continue with the script...

写得有点粗糙,但很容易理解(一旦你知道(。

最新更新