从需要stdin的子进程实时打印stdout

问题描述 投票:20回答:3

这是对this question的跟进,但如果我想将stdin的参数传递给subprocess,我怎样才能实时获得输出?这就是我现在拥有的;我也尝试用Popen模块中的call替换subprocess,这只会导致脚本挂起。

from subprocess import Popen, PIPE, STDOUT
cmd = 'rsync --rsh=ssh -rv --files-from=- thisdir/ servername:folder/'
p = Popen(cmd.split(), stdout=PIPE, stdin=PIPE, stderr=STDOUT)
subfolders = '\n'.join(['subfolder1','subfolder2'])
output = p.communicate(input=subfolders)[0]
print output

在前一个问题,我没有通过stdin我被建议使用p.stdout.readline,那里没有空间管道任何东西到stdin

附录:这适用于转移,但我只看到输出的结尾,我希望看到转移的细节发生时。

python subprocess
3个回答
33
投票

为了实时从子进程中获取stdout,您需要确切地确定您想要的行为;具体来说,您需要决定是要逐行还是逐个字符地处理输出,以及是否要在等待输出时阻塞或在等待时能够执行其他操作。

看起来你的情况可能就是以行缓冲方式读取输出,阻塞直到每个完整的行进入,这意味着subprocess提供的便利功能足够好:

p = subprocess.Popen(some_cmd, stdout=subprocess.PIPE)
# Grab stdout line by line as it becomes available.  This will loop until 
# p terminates.
while p.poll() is None:
    l = p.stdout.readline() # This blocks until it receives a newline.
    print l
# When the subprocess terminates there might be unconsumed output 
# that still needs to be processed.
print p.stdout.read()

如果需要写入进程的stdin,只需使用另一个管道:

p = subprocess.Popen(some_cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
# Send input to p.
p.stdin.write("some input\n")
p.stdin.flush()
# Now start grabbing output.
while p.poll() is None:
    l = p.stdout.readline()
    print l
print p.stdout.read()

另一个答案是,没有必要间接通过文件来传递输入到子进程。


3
投票

我认为这样的事情

from subprocess import Popen, PIPE, STDOUT

p = Popen('c:/python26/python printingTest.py', stdout = PIPE, 
        stderr = PIPE)
for line in iter(p.stdout.readline, ''):
    print line
p.stdout.close()

使用迭代器将基本上返回实时结果..

为了向stdin发送输入,你需要类似的东西

other_input = "some extra input stuff"
with open("to_input.txt","w") as f:
   f.write(other_input)
p = Popen('c:/python26/python printingTest.py < some_input_redirection_thing', 
         stdin = open("to_input.txt"),
         stdout = PIPE, 
         stderr = PIPE)

这将类似于linux shell命令

%prompt%> some_file.o < cat to_input.txt

看阿尔卑斯回答更好的传递给斯坦丁


2
投票

如果在开始读取输出之前传递所有输入,并且如果通过“实时”表示子进程刷新其stdout缓冲区时:

from subprocess import Popen, PIPE, STDOUT

cmd = 'rsync --rsh=ssh -rv --files-from=- thisdir/ servername:folder/'
p = Popen(cmd.split(), stdout=PIPE, stdin=PIPE, stderr=STDOUT, bufsize=1)
subfolders = '\n'.join(['subfolder1','subfolder2'])
p.stdin.write(subfolders)
p.stdin.close() # eof
for line in iter(p.stdout.readline, ''):
    print line, # do something with the output here
p.stdout.close()
rc = p.wait()
© www.soinside.com 2019 - 2024. All rights reserved.