首先,bat脚本要实现后台运行,代码如下:
C:\Users\linuxbugs\Desktop\demo\run_demo.bat
@echo off
if "%1" == "h" goto begin
mshta vbscript:createobject("wscript.shell").run("%~nx0 h",0)(window.close)&&exit
:begin
python %CD%\main.py
然后我用python调用该脚本,并置于后台,不阻塞python继续向下运行
import os
def run():
os.chdir(r'C:\Users\linuxbugs\Desktop\demo')
os.popen('run_demo.bat')
if __name__ == '__main__':
run()
print("xxxxxx") # 会直接打印 xxxxxx run函数并不会阻塞