popen4 doesn't block?

Terry Hancock hancock at anansispaceworks.com
Fri Jan 24 18:47:41 EST 2003


Hi,
I've noticed that "os.popen4" (on Linux) does not block. I'm seeing side 
effects of the process continuing after the call returns, and that's not 
what I want. I need to wait until the sub-process is complete before 
continuing (I think os.system does this, but then I can't get to the input 
and output streams). How can I acheive that?

Some background (and maybe you have a better strategy for solving this 
problem?):

I'm running into a problem with an installation script I'm writing. The 
idea is that I have a whole list of packages that have to be installed as 
dependencies, so I want to automate the process a bit, while still having 
some ability to catch errors.

Each package needs something simple like (say):

"""
tar zxf Packages/megane*
mv megane* megane
cd megane
./configure --prefix=/my/prefix  >& megane.c.log
make  >& megane.m.log
make install >& megane.i.log
"""

Which I *could* translate into python (with os.* calls), but would rather 
leave as a shell script, since it's simpler that way.  So I have a data 
structure in Python that defines my package and includes this script as a 
string.  I then run the script using os.popen4, like this:

i1f, o1f = os.popen4('tcsh')
i1f.write(script)
i1f.close()

I also tried:

os.system("sync")
o1f.flush()

I also thought of "o1f.close()", which seems like it would block, but which 
would then not let me read the output from the script, which I want to keep 
to examine afterwards.

but that doesn't really seem to be the right thing.

After this step, my program should pause and give the user options to 
examine the output log files that were created (like "megane.m.log").  
That's the nice part about using Python for this.

The trouble is, I'm getting control back before these files are completed, 
which makes a mess because the program crashes when I try to open the files 
while they're still open.   (I don't want to explicitly check these files 
because they are not the same for every package -- I want to detect their 
presence in order to offer them as choices).

There must be some way I can wait until the script has finished before 
proceeding, but I haven't been able to figure it out yet.  (Or maybe 
there's a better approach to the whole problem?)

Thanks for any ideas,
Terry

-- 
Anansi Spaceworks
http://www.anansispaceworks.com




More information about the Python-list mailing list