Reading stdout from one program in another program

谁都会走 提交于 2019-12-14 02:17:06

问题


I have a problem concerning the reading of the stdout in python. I have to explain a bit what I am trying to do.

I have a Python program (foo); this calls a second python program (bar). bar will give back to stdout status details and other information to be logged. The foo now has to read this. In principle it works. I can grab the output of bar and send it to a log file without any problems. The problems start when I try to look for certain phrases in the output from bar (the status report).

I am reading the output with os.read(fn, n) from the stdout of bar. The problem is that os.read reads n bytes, but not a line; it can be more or less. I want it to read every line that is written to stdout by bar, these lines end with \n and are written either with print or with sys.stdout.write followed by a sys.stdout.flush().

foo calls bar using:

bar= subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
fd_in[0] = bar.stdout.fileno()
fd_in[1] = bar.stderr.fileno()

My code looks like this at the moment. bar writes:

print 'TAG INFO1 INFO2 INFO3'
#or
sys.stdout.write('TAG INFO1 INFO2 INFO3\n')
sys.stdout.flush()

I am reading in foo with

buf = os.read(fd_in[0], 256)

When I write this to a log file with:

int_logFile.write(buf)
int_logFile.flush()

everything is fine. But if I want to grab the first word of the output line like this:

tokens = buf.strip('\n').split(' ')

I end up in missery, because I do not get TAG, but I might have INFOx or something from a previous output nothing to do with my status messages.

So to sum it up I need to read the output of bar line by line in foo. Any ideas???


回答1:


Er, bar.stdout.readline()? Going all the way to file descriptors is rarely necessary.




回答2:


How about Popen.communicate? This will also let you send input to the process if you want to.



来源:https://stackoverflow.com/questions/7808345/reading-stdout-from-one-program-in-another-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!