[Tutor] piping continuous output

Koen Bossers koen@behindthesofa.dhs.org
Fri, 27 Jul 2001 11:52:04 +0200


OK, for your information,

I solved the problem. It appears that print_messages.py does not print immediately after the
print statement due to the popen2 routine. Adding the line 'sys.stdout.flush()' after the
print statement made the delays dissappear.

Cheers,

Koen Bossers



Koen Bossers wrote:

> By the way, adding time.sleep(11) before entering the loop eliminates the time differences
> between outputs. They're nicely aligned at 0.2 secs. right now. Only that darned 10 second
> pause....
>
> Koen Bossers wrote:
>
> > sill@optonline.net wrote:
> >
> > > Look at pctail.py program on vaults of parnassus, it does something
> > similar
> > > (reads from growing log files).
> >
> > Not what I had in mind. I want to avoid creating temporary files. That's
> > why I
> > chose popen, else I would have used something like os.system('command >>
> >
> > /tmp/tempfile') or something.
> >
> > OK second try: some VERY weird behaviour here:
> >
> > [print_messages.py]
> > #! /usr/bin/python
> >
> > import time, os
> >
> > counter = 0
> > while counter < 5000:
> >
> >     print '%d'%counter
> >
> >     time.sleep(0.01)
> >     counter = counter + 1
> >
> > [popentest.py]
> > #! /usr/bin/python
> >
> > ## test popen with continuing output
> >
> > import os, time, string
> > import popen2
> >
> > child = popen2.Popen3('./print_messages.py')
> > output = child.fromchild
> >
> > count = 1
> >
> > print "starting...", os.getpid()
> > listing = ''
> > start = time.time()
> >
> > while child.poll() == -1:        ## if child is still running...
> >
> >     print count, time.time()-start
> >     test = output.read(100)
> >
> >     count = count + 1
> >     time.sleep(0.2)
> >
> > output when i run popentest.py:
> >
> > starting... 414
> > 1 after 0.000128984451294
> > 2 after 10.696469903
> > 3 after 10.896476984
> > 4 after 11.0964080095
> > 5 after 11.2964220047
> > 6 after 11.4964289665
> > 7 after 11.6964190006
> > 8 after 11.8964159489
> > 9 after 12.0963799953
> > 10 after 12.2964549065
> > 11 after 12.4964559078
> > 12 after 12.6964579821
> > 13 after 12.8963899612
> > etc...
> >
> > Notice the major timediff between step 1 and 2! 10 seconds!!!! what the
> > hell
> > is happening here? I know Python isn't always that fast, but why this
> > big
> > delay? Enlighten me!
> >
> > Cheers,
> >
> > Koen Bossers
> >
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor