Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/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()不捕获supprocess标准输出_Python_Subprocess - Fatal编程技术网

Python communicate()不捕获supprocess标准输出

Python communicate()不捕获supprocess标准输出,python,subprocess,Python,Subprocess,cmd打印/输出:stderr使用print>>sys.stderr和stdout使用print import subprocess def ExecuteAndGetValue(cmd, v): PrintCmd(cmd, v) sub = subprocess.Popen(cmd, shell = True, stdout=subprocess.PIPE) result = sub.communicate() sub

cmd
打印/输出:stderr使用
print>>sys.stderr
和stdout使用
print

import subprocess    
def ExecuteAndGetValue(cmd, v):
        PrintCmd(cmd, v)
        sub = subprocess.Popen(cmd, shell = True, stdout=subprocess.PIPE)  
        result = sub.communicate()
        sub.wait()
        return result

import subprocess    
def ExecuteAndGetValue2(cmd, v):
    PrintCmd(cmd, v)
    sub = subprocess.Popen(cmd, shell = True, stdout=subprocess.PIPE)  
    result = sub.communicate()[0]
    return result


[aa, err] = Util.ExecuteAndGetValue(cmd, args.v)  # run half an hour, the output size is about 15MB
[bb, err] = Util.ExecuteAndGetValue(cmd, args.v)  # run half an hour, the output size is about 15MB
print aa  // aa is empty
print bb // bb is empty
cmd有一个循环,其中print
stderr
info。最后,它将所有输出打印到
result

(1) 如果
cmd
打印较小尺寸的字符串,则效果良好<代码>aa和
bb
显示正确的结果

(2) 如果
cmd
打印大尺寸,如10MB,
aa
bb
为空

(3) 基于上述测试,我认为sub.communicate()[0]是否实时从Popen读取标准输出?如果它是一个大的标准件,它不能读取它吗?有人能再解释一下吗

此外,我希望在屏幕上实时看到stderr或错误消息。如果我使用以下命令,它不会实时显示stderr:

result = []
for i in range(n):
    print >> sys.stderr, "T_index: " + str(T_index)
    bb = func(i)
    result.append(bb)
print '\n'.join(result)

我如何解决这个问题?它可能与缓冲区大小有关?谢谢

您需要在Popen调用中使用stderr参数

import subprocess    
def ExecuteAndGetValue(cmd, v):
        PrintCmd(cmd, v)
        sub = subprocess.Popen(cmd, shell = True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)  
        result = sub.communicate()
        print result[1]
        return result

您不需要使用sub.wait()-communicate()等待命令执行完成并返回输出/错误消息。

我想在控制台中实时打印错误信息。因此,如果我添加stderr=subprocess.PIPE。它不打印信息。如何实时打印错误信息?subprocess.communicate()返回输出和错误消息。如果无法打印错误消息,则可能没有错误。为了回答关于缓冲区大小的另一个问题,Communication应该能够刷新缓冲区中的信息,并且不会导致任何死锁。查看更多信息。谢谢,我添加了一些代码来显示
cmd
如何在后台运行,它使用一个循环,每个循环打印stderr,但是,如果我使用
ubprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
它在
cmd
运行时不会在每个循环中打印结果。
sub = subprocess.Popen(cmd, shell = True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return sub.communicate()