相关疑难解决方法(0)

逐行读取子进程标准输出

我的python脚本使用subprocess来调用非常嘈杂的linux实用程序.我想将所有输出存储到日志文件中并向用户显示一些输出.我认为以下内容可行,但在实用程序产生大量输出之前,输出不会显示在我的应用程序中.

#fake_utility.py, just generates lots of output over time
import time
i = 0
while True:
   print hex(i)*512
   i += 1
   time.sleep(0.5)

#filters output
import subprocess
proc = subprocess.Popen(['python','fake_utility.py'],stdout=subprocess.PIPE)
for line in proc.stdout:
   #the real code does filtering here
   print "test:", line.rstrip()
Run Code Online (Sandbox Code Playgroud)

我真正想要的行为是过滤器脚本在从子进程接收时打印每一行.Sorta就像tee使用python代码一样.

我错过了什么?这甚至可能吗?


更新:

如果将a sys.stdout.flush()添加到fake_utility.py,则代码在python 3.1中具有所需的行为.我正在使用python 2.6.您会认为使用proc.stdout.xreadlines()将与py3k一样工作,但事实并非如此.


更新2:

这是最小的工作代码.

#fake_utility.py, just generates lots of output over time
import sys, time
for i in range(10):
   print i
   sys.stdout.flush()
   time.sleep(0.5)

#display out put line by …
Run Code Online (Sandbox Code Playgroud)

python subprocess

216
推荐指数
8
解决办法
32万
查看次数

子进程命令的实时输出

我正在使用python脚本作为流体动力学代码的驱动程序.当运行模拟时,我subprocess.Popen用来运行代码,从stdout和stderr收集输出到subprocess.PIPE---然后我可以打印(并保存到日志文件)输出信息,并检查是否有任何错误.问题是,我不知道代码是如何进展的.如果我直接从命令行运行它,它会给我输出关于它在什么时间迭代,什么时间,下一个时间步骤是什么等等的输出.

有没有办法既存储输出(用于记录和错误检查),还产生实时流输出?

我的代码的相关部分:

ret_val = subprocess.Popen( run_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True )
output, errors = ret_val.communicate()
log_file.write(output)
print output
if( ret_val.returncode ):
    print "RUN failed\n\n%s\n\n" % (errors)
    success = False

if( errors ): log_file.write("\n\n%s\n\n" % errors)
Run Code Online (Sandbox Code Playgroud)

最初我正在run_command通过管道,tee以便副本直接进入日志文件,并且流仍然直接输出到终端 - 但这样我就不能存储任何错误(对我的知识).


编辑:

临时解决方案:

ret_val = subprocess.Popen( run_command, stdout=log_file, stderr=subprocess.PIPE, shell=True )
while not ret_val.poll():
    log_file.flush()
Run Code Online (Sandbox Code Playgroud)

然后,在另一个终端,运行tail -f log.txt(st log_file = 'log.txt').

python error-handling shell logging subprocess

165
推荐指数
8
解决办法
13万
查看次数

python,subprocess:从子进程读取输出

我有以下脚本:

#!/usr/bin/python

while True:
    x = raw_input()
    print x[::-1]
Run Code Online (Sandbox Code Playgroud)

我叫它ipython:

In [5]: p = Popen('./script.py', stdin=PIPE)

In [6]: p.stdin.write('abc\n')
cba
Run Code Online (Sandbox Code Playgroud)

它工作正常.

但是,当我这样做时:

In [7]: p = Popen('./script.py', stdin=PIPE, stdout=PIPE)

In [8]: p.stdin.write('abc\n')

In [9]: p.stdout.read()
Run Code Online (Sandbox Code Playgroud)

口译员挂了.我究竟做错了什么?我希望能够多次写入和读取另一个进程,将一些任务传递给此进程.我需要做些什么不同的事情?

编辑1

如果我使用communicate,我得到这个:

In [7]: p = Popen('./script.py', stdin=PIPE, stdout=PIPE)

In [8]: p.communicate('abc\n')
Traceback (most recent call last):
  File "./script.py", line 4, in <module>
    x = raw_input()
EOFError: EOF when reading a line
Out[8]: ('cba\n', None)
Run Code Online (Sandbox Code Playgroud)

编辑2

我试着冲洗:

#!/usr/bin/python

import sys …
Run Code Online (Sandbox Code Playgroud)

python subprocess stdout

13
推荐指数
1
解决办法
2万
查看次数

标签 统计

python ×3

subprocess ×3

error-handling ×1

logging ×1

shell ×1

stdout ×1