【问题标题】:subprocess.check_output is too slow on script python but not in interactive python shellsubprocess.check_output 在脚本 python 上太慢,但在交互式 python shell 中不是
【发布时间】:2020-09-25 16:39:25
【问题描述】:

我的问题是关于子流程的。

我正在使用 subprocess 调用外部程序,我使用了 check_output() 方法,在该方法中我将 args 作为列表传递。我注意到的是,从交互式 shell 使用 check_output() 需要 3 分钟(在我的情况下,这是执行外部程序的正确时间[如果进程保持阻塞等待响应没有问题])但是当我在 python 脚本中使用具有相同参数的相同方法时,可能需要长达 1 小时!。

以前有没有人遇到过这种情况?有什么建议吗?

有关我在 Debian 10 上使用 Python3.7.3 的信息。

先谢谢了

----编辑:

我的代码没有什么特别之处

我的脚本只是:

from subprocess import check_output
try:
    #commandList is the programm (external bin) I would excute with its params
    result = check_output(commandList) 
    print(result.decode("latin-1"))
except Exception as e:
    print(e)

【问题讨论】:

  • 你还在 Python 脚本中做其他工作吗?尝试创建一个可重现的最小示例。
  • @IonutTicus 检查我的编辑 :) 这只是一个简单的 check_output 没有什么特别之处
  • 如果你用subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)运行它会发生什么?
  • 感谢@SpiderPig1297 我已经使用 subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True) 和 Communicate 解决了这个问题

标签: python python-3.x linux subprocess debian


【解决方案1】:

使用subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess. STDOUT, shell=True) 解决了这个问题。

【讨论】:

  • 这个解决方案很难找到,但效果很好。
猜你喜欢
  • 2016-05-29
  • 2011-08-31
  • 2017-02-13
  • 1970-01-01
  • 2017-11-21
  • 1970-01-01
  • 2023-04-10
  • 1970-01-01
  • 2018-07-02
相关资源
最近更新 更多