writing consecutive data to subprocess command 'more'

SanPy jhmsmits at gmail.com
Sun May 3 03:06:37 EDT 2009


Hmm, it works as long as the pager command is available on the system.
I created a test file to explain what I mean. You can find it here:
http://gist.github.com/105880

The 'pager' command is on purpose a command that is not available on
the system.
It should fall back to sys.stdout in the write method. However, it
does not show any output at all.
Strangely enough, when a put a time.sleep(1) between the first and
second printer.write statements, the second and third statement do
appear.
Btw, the normal pager command works fine on my system.
How can I solve this, so that the fallback sys.stdout does show
output?
---
Sander

On 2 mei, 23:00, SanPy <jhmsm... at gmail.com> wrote:
> Thanks, that works beautifully!
>
> Regards,
> Sander
>
> On 2 mei, 22:35, "Gabriel Genellina" <gagsl-... at yahoo.com.ar> wrote:
>
> > communicate writes to the child's stdin and waits for it to finish. If you  
> > want to keep writing, don't use communicate. And you'll need to keep state  
> >  from one call to another, so use a class. Based on the code above, create  
> > a class Pager with __init__, write and close methods:
>
> > class Pager:
> >   def __init__(self):
> >    # copy the logic above
> >    self.proc = subprocess.Popen(...)
> >    self.file = self.proc.stdin
> >    # if something goes wrong, set self.proc=None and self.file=sys.stdout
>
> >   def write(self, text):
> >    self.file.write(text)
>
> >   def close(self):
> >    if self.proc:
> >     self.file.close()
> >     self.proc.wait()
>
> > Also, take a look at the pager function in the pydoc module (see the  
> > source) - it handles several cases.
>
> > --
> > Gabriel Genellina




More information about the Python-list mailing list