Ret status technique popen, popen2 (NT)

Philip Thorne pbt at pipeline.com
Fri Jun 16 00:58:07 EDT 2000


Can anyone give me a pointer on how to monitor the status of win32pipe
popen & popen2 calls? I want to get the return stat of the popen job
and to be able to detect if the popen2 job worked/started.

The os.* versions return the status's as expected on .close() but I
don't want to be tied to console apps so am using the win32pipe (129)
versions. These run ok but appear to return None in all situations (on
NT SP5 anyway).

The intent of my app is to pipe a process's output thru a second whose
task it is to analyse and react to the former's output _as it's
produced_. On completion the monitoring process outputs a message to
stdout that I need to catch.

I can get the status out of the popen job by means of 
   q = popen('job1 & echo job1 returned %ERRORLEVEL%')

which also serves to detect if the job failed to run at all but this
seems clunky. [As does reading from the popen line by line in a loop
to feed the popen2]. 

If the 2-way pipe fails to kick-off it takes a while before things go
wrong (if they do) e.g. I can still write to it:

>>> a,b=popen2('nosuch','t')
>>> print a
<open file 'nosuch', mode 'w' at 798e80>
>>> print b
<open file 'nosuch', mode 'r' at 798ec0>
>>> a.write('swallow\n')
>>> a.flush()
>>> a.close()

Although later on I (sometimes?) get an I/O Error on a write
complaining about an invalid parameter (itself confusing as I find I
can write an empty string '' without complaint immediately after
getting the error - which unfortunately I can't paste in as I typing
from memory).

It's simple to do using a co-process in a ksh (UWIN) which is sad as
I've been advocating python as a quick dev tool for NT & *nix (and in
this situation ksh won't do long-term).

Any ideas appreciated. ta
Phil B. 



More information about the Python-list mailing list