os.popen

Discussion in 'Python' started by Bart Nessux, Feb 19, 2004.

  1. Bart Nessux

    Bart Nessux Guest

    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()
     
    Bart Nessux, Feb 19, 2004
    #1
    1. Advertising

  2. Bart Nessux

    Guest

    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
     
    , Feb 19, 2004
    #2
    1. Advertising

  3. In article <>, <> 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()

    .
    .
    .
    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 ...
    --

    Cameron Laird <>
    Business: http://www.Phaseit.net
     
    Cameron Laird, Feb 19, 2004
    #3
  4. Bart Nessux

    Guest

    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
     
    , Feb 19, 2004
    #4
  5. Bart Nessux

    Bart Nessux Guest

    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
     
    Bart Nessux, Feb 19, 2004
    #5
  6. 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
     
    Thomas Guettler, Feb 19, 2004
    #6
  7. 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 && && 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_
     
    Erik Max Francis, Feb 20, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ming
    Replies:
    2
    Views:
    398
    Default User
    Aug 14, 2003
  2. Grumble

    popen() equivalent

    Grumble, Nov 25, 2003, in forum: C++
    Replies:
    3
    Views:
    13,892
    Grumble
    Nov 26, 2003
  3. Using popen

    , Feb 26, 2004, in forum: C++
    Replies:
    1
    Views:
    3,339
    Unforgiven
    Feb 26, 2004
  4. sam

    popen and cin

    sam, May 3, 2005, in forum: C++
    Replies:
    1
    Views:
    518
    Artie Gold
    May 3, 2005
  5. File.popen/IO.popen

    , May 20, 2006, in forum: Ruby
    Replies:
    1
    Views:
    272
    Robert Klemme
    May 20, 2006
Loading...

Share This Page