473,406 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 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 15939
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 " + u"\u0054\u0045\u0053\u0054.txt") print f.read () print "2nd popen..."...
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: #include <stdio.h> #include <sys/types.h>...
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. Then via Popen it starts a pythons search script,...
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 script is invoked from. eg: C:\type foo.py import...
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" "http://search.cpan.org" and capture all it's...
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 external command (I have been able to do that with...
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: #include <stdio.h> main() { printf ("Hello...
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 for a function that created a subprocess, but the...
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 written a class to work with the input and output...
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 set output = `cat this | grep that | whatever...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.