os.popen

B

Bart Nessux

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')
ping.read()
ping.close()
 
P

P

Bart said:
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()
 
C

Cameron Laird

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.


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")


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()
.
.
.
1. Why
sh ./ping.sh ...
rather than
./ping.sh ...
?
2. For more typical processes that launch and run to
completion, yes, to the best of my knowledge, it
*is* feasible to simplify
op = os.popen(command)
op.read()
op.close()
to just
op = os.popen(command)
op.close()
I need to study the Windows-side implementation a bit
more ...
 
P

P

Cameron said:
1. Why
sh ./ping.sh ...
rather than
./ping.sh ...
?

Maybe he didn't make ping.sh executable for some reason.
Not relevant to this discussion.
2. For more typical processes that launch and run to
completion, yes, to the best of my knowledge, it
*is* feasible to simplify
op = os.popen(command)
op.read()
op.close()
to just
op = os.popen(command)
op.close()

iff the command doesn't fill the buffer.
If it does then it will not run to completion.
 
B

Bart Nessux

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.



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")


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()

Thank you for that explanation. It makes sense. When you say loop the
read do you mean something like this:

ping = os.popen('sh ./ping.sh')
while 1:
ping.read()
ping.close()

I liked the /dev/null examples too as I don't need output from some of
the functions. I didn't know I could throw output away like that.

Thanks,
Bart
 
T

Thomas Guettler

Am Thu, 19 Feb 2004 10:18:56 -0500 schrieb Bart Nessux:
Thank you for that explanation. It makes sense. When you say loop the
read do you mean something like this:

ping = os.popen('sh ./ping.sh')
while 1:
ping.read()
ping.close()

I liked the /dev/null examples too as I don't need output from some of
the functions. I didn't know I could throw output away like that.

ping.read() will wait until EOF (End of File). You should
call it only once. This means ping.read() will return
as soon as the ping stopped pingging.

I think you can give the number of bytes to read
as an argument.

thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top