[Tutor] piping continuous output
Koen Bossers
koen@behindthesofa.dhs.org
Wed, 25 Jul 2001 12:39:22 +0200
Hi,
I'm trying some stuff with popen2.
The thing is that I want to parse the output from a program (will be the
output of cdrecord or similar progams) that runs for a while (in this
example indefinitely) and print out a progress indicator or something
like that. For this purpose I created the following two scripts:
[print_messages.py]
#! /usr/bin/python
import time, os
counter = 0
while 1:
counter += 1
print 'Hello from process', os.getpid()
print '%d'%counter,
time.sleep(0.05)
[popentest.py]
#! /usr/bin/python
## test popen with continuing output
import os, time
import popen2
(output, input) = popen2.popen2('./print_messages.py')
count = 0
print "starting...", os.getpid()
listing = ''
while 1:
test = output.read(1000) ## read next 1000 bytes
listing += test
if not test: ## wait for sufficient output
time.sleep(0.5)
listing = ''
print listing
print "\nRun",count
count += 1
This works for me. The only thing is I find this a very dirty hack. Are
there other ways to achieve the same result?
Thanks,
Koen Bossers