为了账号安全,请及时绑定邮箱和手机立即绑定

subprocess.check_output 在脚本 python 上太慢,但在交互式

subprocess.check_output 在脚本 python 上太慢,但在交互式

手掌心 2023-09-05 20:16:08
我的问题是关于子流程的。我正在使用子进程来调用外部程序,我使用了 check_output() 方法,其中我将参数作为列表传递。我注意到,当从交互式 shell 中使用 check_output() 时,需要 3 分钟(在我的情况下,这是执行外部程序的正确时间 [如果进程保持阻塞等待响应,则没有问题])当我在 python 脚本中使用相同的方法和相同的参数时,可能需要长达 1 小时!以前有人遇到过这种情况吗?有什么建议吗?有关信息,我在 Debian 10 上使用 Python3.7.3。感谢是提前的- - 编辑:我的代码没有什么特别的我的脚本只是:from subprocess import check_outputtry:    #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)
查看完整描述

1 回答

?
30秒到达战场

TA贡献1828条经验 获得超6个赞

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



查看完整回答
反对 回复 2023-09-05
  • 1 回答
  • 0 关注
  • 94 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信