popen2.popen2,3,4 from Python 2.0 on NT 4.0 SP5

Paul Moore paul.moore at uk.origin-it.com
Thu Oct 26 06:04:08 EDT 2000


On Thu, 26 Oct 2000 05:11:24 +0200, David Bolen <db3l at fitlinxx.com>
wrote:

>Mark Tompkins <mdtompkins at home.com> writes:
>
>> I was doing some testing, and have hit the wall right off the bat.  When
>> I use popen2.popenX('cmd') to call the sqlplus utility, reading stdout
>> of the cmd appears to hang when the read is past the last character in
>> the buffer, or when there is no data (as what happens, if there is an
>> error)????
[...]
>Alternatively, the sqlplus utility could be waiting for stdin to close
>for some reason - if you aren't going to be feeding input to the child
>process that way, I'd suggest closing that handle, or not using a
>popen# that gives it to you.  Unless you've found that sqlplus doesn't
>run without a valid stdin.

If SQL*Plus is given a nonexistent script file, it reports an error
and then goes into interactive mode. (In fact, if it is given a valid
script which doesn't finish with an exit command, it also goes into
interactive mode after running the script).

To quit SQL*Plus, and hence get an EOF on stdout (which allows your
stdout read to complete) you should close stdin (or send an exit
command on it). Remember, the pipes are buffered, so even if you have
some output, it may not be available to you because the pipe is
waiting for EOF or for the buffer to fill.

This is why popen2-style calls are prone to deadlock. You *have* to be
careful of buffering issues and the like.

But to summarise, close the stdin pipe before reading from the stdout
pipe. (Actually, in this case, if you aren't writing anything to
stdin, why use popen2 at all?)

Hope this helps,
Paul




More information about the Python-list mailing list