os.popen

P at draigBrady.com P at draigBrady.com
Thu Feb 19 09:20:19 EST 2004


Bart Nessux wrote:
> When using os.popen, at what point is the process actually started? Take 
> the example below... would the first or second line actually execute the 
> command? If it's the first line, then why would I want to use the second 
> line at all unless I wanted to see on the console what happened?
> 
> ping = os.popen('sh ./ping.sh')

this will run until the buffer between the ping process and the python
app is full. This is 4k under linux. Then the ping process will block
until data is read as there is no where to put it's output.

> ping.read()

This will read what's currently in the buffer between the processes.
You will need to do this in a loop.

alternatively if you don't care about the output then you
can throw it away, and hence remove the need for the ping.read() like:

ping = os.popen("sh ./ping.sh > /dev/null")

 > ping.close()

This will wait for the ping process to finish.

To tell it to finish you will need to do something like:
   ping = popen2.Popen3("sh ./ping.sh > /dev/null")
   os.kill(ping.pid, signal.SIGTERM)
   ping.wait()

-- 
Pádraig Brady - http://www.pixelbeat.org




More information about the Python-list mailing list