[Tutor] piping continuous output

ak@silmarill.org ak@silmarill.org
Wed, 25 Jul 2001 08:17:41 -0400


On Wed, Jul 25, 2001 at 12:39:22PM +0200, Koen Bossers wrote:
> 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:

progress indicator for infinity? How high is your monitor's resolution? :-)

> 
> 
> [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
> 

Look at pctail.py program on vaults of parnassus, it does something similar
(reads from growing log files).
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

-- 
Cymbaline: intelligent learning mp3 player - python, linux, console.
get it at: http://silmarill.org/cymbaline