Python:subprocess.Popen()无法在Git Bash / MinGW中执行

时间:2018-08-08 23:03:33

标签: python subprocess

我想在Python脚本中使用subprocess.Popen()异步启动多个shell命令,以便脚本可以继续运行而不必等待每个命令完成。该脚本在Windows cmd窗口或Anaconda Prompt中运行良好。但是当我在Git Bash中运行它时,Popen()无法启动shell命令。在Git Bash中将Popen()更改为run()可以正常工作,但这不是我的预期设计。

关于Git Bash或MinGW是否有任何导致执行subprocess.Popen()失败的特性?

import shlex
import subprocess
from pathlib import Path

script = Path(script_path).as_posix()
for opt in options:
    shell_cmd = f'python {script} -option {opt}'
    p = subprocess.run(shlex.split(shell_cmd))    # run() works
    p = subprocess.Popen(shlex.split(shell_cmd))  # Popen() doesn't work when this script is ran inside Git Bash. It works fine inside Windows cmd or Anaconda Prompt.

0 个答案:

没有答案