By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,100 Members | 2,892 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,100 IT Pros & Developers. It's quick & easy.

os.popen

P: n/a
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()

Jul 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
P
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

Jul 18 '05 #2

P: n/a
In article <40**************@draigBrady.com>, <P@draigBrady.com> wrote:
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()

Jul 18 '05 #3

P: n/a
P
Cameron Laird wrote:
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.

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

Jul 18 '05 #4

P: n/a
P@draigBrady.com wrote:
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()


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

Jul 18 '05 #5

P: n/a
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
Jul 18 '05 #6

P: n/a
P@draigBrady.com wrote:
Maybe he didn't make ping.sh executable for some reason.
Not relevant to this discussion.


Or it has a broken/missing bangpath.

--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
\__/ You and I / We've seen it all / Chasing our hearts' desire
-- The Russian and Florence, _Chess_
Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.