【问题标题】:Python Subprocess.Popen doesn't run the ProcessPython Subprocess.Popen 不运行进程
【发布时间】:2021-09-09 20:17:38
【问题描述】:

您好,我正在尝试通过 python 打开一个进程。

如果我使用的是subprocess.callsubprocess.check_call,它会按我的预期工作。

唯一的问题是即使执行的进程完全“启动”,我的程序也会停止工作。

process.Popen 只是不起作用,我怎样才能让process.Popen 运行?

clientPath = "C:\\Program Files (x86)\\SomeFolder\\SomeExecutable.exe"
subprocess.Popen(clientPath, stdout=subprocess.PIPE)

这对我不起作用

找不到其他问题的帮助。

【问题讨论】:

  • 你怎么知道子进程没有运行?

标签: python subprocess executable


【解决方案1】:

subprocess.Popen 应该给你一个结果,进程句柄。 尝试将更改代码添加到

clientPath = "C:\\Program Files (x86)\\SomeFolder\\SomeExecutable.exe"
process = subprocess.Popen(clientPath, stdout=subprocess.PIPE)
print('Process ID', process)
print('Is Process alive', process.poll())

作为一个不错的选择,您可以使用子流程包装器,即command_runner

使用pip install command_runner安装

运行

from command_runner import command_runner

cmd = r"C:\Program Files (x86)\SomeFolder\SomeExecutable.exe"
exit_code, output = command_runner(cmd)
print(exit_code)
print(output)

然后您可以确保您的程序不会运行太久以确保您的程序不会在某处阻塞:

exit_code, output = command_runner(cmd, timeout=300)

【讨论】:

    猜你喜欢
    • 2019-08-02
    • 1970-01-01
    • 2022-01-06
    • 2017-07-27
    • 1970-01-01
    • 2013-08-11
    • 2015-07-09
    • 1970-01-01
    • 2019-08-01
    相关资源
    最近更新 更多