Use of os.popen3() under WinNT4

David Niergarth jdnier at execpc.com
Fri Jan 26 16:12:06 EST 2001


I would like to be able to run a command in a subprocess under WinNT4 and
read/write (as appropriate) to stdout, stdin, and stderr. I'm trying to
understand how to use os.popen3(). Perhaps I'm misunderstanding it's purpose
or it's behavior is different under NT? I'm using Python 2.0 so the old
Windows popen problem shouldn't be an issue.

In my current case, I want to run Ghostscript (gswin32c.exe) and need to be
able to write to the process and, hopefully, capture its stdout. In the
following example:

>>> import os
>>> command = 'gswin32c -help'
>>> child_stdout, child_stdin, child_stderr = os.popen3(command, mode='b')

child_stdout ends up as a file descriptor opened in write mode, while
child_stdin is a file descriptor opened in read mode; in and out seems
backwards here. In the above command, Ghostscript prints a help message and
quits. To get at the help message, you need to read from stdin.

>>> child_stdin.read()
"6.01 (2000-03-17)\012Copyright (C) 2000 Aladdin... [etc.]"

>>> child_stderr.read()
''

So is it just an issue of perspective? (I found this confusing.)

More importantly, if you run a command that doesn't immediately terminate,
trying to read from child_stdin or child_stderr before doing a
child_stdout.close() causes a hang. (The only way I've found to recover is
to kill the parent Python process.) Is there a way to tell if the process
has terminated before trying a .read() or, better yet, is there a way to
signal or force the process to finish?

Any elaborations or pointers to code examples would be appreciated. I'll
sumarize if I get many responses.

Thanks,

--David Niergarth
jdnier at execpc.com




More information about the Python-list mailing list