Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python子进程communicate()方法速度慢_Python_Shell_Python 3.x_Subprocess - Fatal编程技术网

python子进程communicate()方法速度慢

python子进程communicate()方法速度慢,python,shell,python-3.x,subprocess,Python,Shell,Python 3.x,Subprocess,我有以下代码,communicate()方法返回输出的时间比预期的要长得多(大约2秒)。我是python新手,已经尽力了。谁能告诉我发生了什么事?!我想我应该在某处刷新stdin或stdout 我正在从这个python脚本调用另一个程序 import sys import os import subprocess os.chdir("megam_0.92") command="./megam -quiet -predict ../training_modelcp -nc multitron .

我有以下代码,
communicate()
方法返回输出的时间比预期的要长得多(大约2秒)。我是python新手,已经尽力了。谁能告诉我发生了什么事?!我想我应该在某处刷新
stdin
stdout

我正在从这个python脚本调用另一个程序

import sys
import os
import subprocess

os.chdir("megam_0.92")
command="./megam -quiet -predict ../training_modelcp -nc multitron ../dummyst.txt"

result={}
result=subprocess.Popen(command,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True,universal_newlines=True,bufsize=0)
k=[]
print("1")
print(result)
print("9")
# sys.stdin.flush()
out=result.communicate()[0]
# result.stdout.flush()
# sys.stdout.flush()
print("2")
print(out)
print("3")
for j in out.split("\n"):
    k.append(j.split("\t",1)[0])
print(k)
print("4")

我可以在毫秒内看到输出中的“9”,在那里大约需要2秒,然后以毫秒为单位再次打印其余的内容

当您从命令行运行命令时,需要多长时间才能完成?通信将等待命令完成。如果您对iter中的行(result.stdout.readline,“”)使用
:print(line)
您看到输出了吗?您需要
shell=True
吗?@PadraicCunningham当我从命令行运行时,它以毫秒为顺序运行。是的,我在代码中看到了输出,但问题是它花费的时间太长。@EtanReisner当我没有使用shell=True时,结果也是一样的。
megam
是否尝试从标准输入读取?当您正常运行命令行时,是否需要点击命令行上的任何内容以使其返回/完成?可以使用
strace
查看从python运行时它在做什么吗?