472,353 Members | 1,419 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

os.popen

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
6 15876
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
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
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@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
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@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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Stuart McGraw | last post by:
When I run (Python 2.3.3) this script on a MS Windows 2000 machine: import os print "1st popen..." f = os.popen ("echo " +...
17
by: bastiaannaber | last post by:
I am trying to write a program which uses popen but I have a problem. I want to detect if the program I call with popen has ended. For example: ...
2
by: Maarten van Veen | last post by:
A long story made short, I've build a python/cgi website consisting of two pages. Page1 has a html form in which you can input a series of queries....
2
by: Greg Ercolano | last post by:
When I use os.popen(cmd,'w'), I find that under windows, the stdout of the child process disappears, instead of appearing in the DOS window the...
3
by: Jesse | last post by:
Hi all, I have a problem using wget and Popen. I hope someone can help. -- Problem -- I want to use the command: wget -nv -O "dir/cpan.txt"...
12
by: Eric_Dexter | last post by:
I am trying to modify a programming example and I am coming up with two problems... first is that I can't seem to pass along the arguments to the...
15
by: Daniel Klein | last post by:
I'm trying to get popen to work on Windows. Here's a simplified example of what I'm trying to get working: I have a hw.c program as follows: ...
25
by: Jeremy Banks | last post by:
Hi. I wondered if anyone knew the rationale behind the naming of the Popen class in the subprocess module. Popen sounds like the a suitable name...
1
by: Mark Shewfelt | last post by:
Hello, I am attempting to use Popen() in a Windows service. I have a small Win32 .exe that I normally run through the os.popen2() function. I've...
3
by: Roger Davis | last post by:
Hi, I am brand-new to Python but am an experienced C/Unix programmer. I am rewriting in Python some old shell scripts that do lots of stuff like ...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.