spawn* or exec* and fork, what should I use and how ?

Discussion in 'Python' started by Lingyun Yang, Dec 16, 2004.

  1. Lingyun Yang

    Lingyun Yang Guest

    Hi,

    I want to use python as a "shell like" program,
    and execute an external program in it( such as mv, cp, tar, gnuplot)
    I tried:

    os.execv("/bin/bash",("/usr/bin/gnuplot",'-c "gnuplot < plot.tmp"'))

    since it's in a for-loop, it should be executed many times, but
    It exits after the first time running.

    so I have to use spawn* like this:

    os.spawnlp(os.P_WAIT, 'gnuplot', 'gnuplot', 'plot.tmp')

    It works very well.

    My question is,

    1. why my exec(..) command doesn't work?
    2. exec* must be with fork ?
    3. In what situation, we choose one over another ?

    Thank you!

    regards,
    Lingyun
     
    Lingyun Yang, Dec 16, 2004
    #1
    1. Advertising

  2. Lingyun Yang

    Peter Hansen Guest

    Lingyun Yang wrote:
    > I want to use python as a "shell like" program,
    > and execute an external program in it( such as mv, cp, tar, gnuplot)
    >
    > os.execv("/bin/bash",("/usr/bin/gnuplot",'-c "gnuplot < plot.tmp"'))


    I would suggest checking out the "subprocess" module,
    new in Python 2.4. It subsumes the functionality
    of most of the alternative methods such as execv and
    spawn and os.system(), and provides an arguably cleaner
    interface.

    -Peter
     
    Peter Hansen, Dec 16, 2004
    #2
    1. Advertising

  3. Lingyun Yang

    Binu K S Guest

    exec calls will replace the script process with the new process.

    >From the execv documentation:

    "These functions all execute a new program, replacing the current
    process; they do not return. On Unix, the new executable is loaded
    into the current process, and will have the same process ID as the
    caller. Errors will be reported as OSError exceptions."

    As Peter suggested, use the subprocess module in Python 2.4.

    On Thu, 16 Dec 2004 03:00:45 GMT, Lingyun Yang <> wrote:
    > Hi,
    >
    > I want to use python as a "shell like" program,
    > and execute an external program in it( such as mv, cp, tar, gnuplot)
    > I tried:
    >
    > os.execv("/bin/bash",("/usr/bin/gnuplot",'-c "gnuplot < plot.tmp"'))
    >
    > since it's in a for-loop, it should be executed many times, but
    > It exits after the first time running.
    >
    > so I have to use spawn* like this:
    >
    > os.spawnlp(os.P_WAIT, 'gnuplot', 'gnuplot', 'plot.tmp')
    >
    > It works very well.
    >
    > My question is,
    >
    > 1. why my exec(..) command doesn't work?
    > 2. exec* must be with fork ?
    > 3. In what situation, we choose one over another ?
    >
    > Thank you!
    >
    > regards,
    > Lingyun
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
     
    Binu K S, Dec 16, 2004
    #3
  4. Lingyun Yang

    Lingyun Yang Guest

    Peter Hansen wrote:
    > Lingyun Yang wrote:
    >
    >> I want to use python as a "shell like" program,
    >> and execute an external program in it( such as mv, cp, tar, gnuplot)
    >>
    >> os.execv("/bin/bash",("/usr/bin/gnuplot",'-c "gnuplot < plot.tmp"'))

    >
    >
    > I would suggest checking out the "subprocess" module,
    > new in Python 2.4. It subsumes the functionality
    > of most of the alternative methods such as execv and
    > spawn and os.system(), and provides an arguably cleaner
    > interface.
    >
    > -Peter


    Thank you!

    I got the document about subprocess,
    http://www.python.org/dev/doc/devel/lib/module-subprocess.html
     
    Lingyun Yang, Dec 16, 2004
    #4
  5. Lingyun Yang

    Keith Dart Guest

    Lingyun Yang wrote:
    > Hi,
    >
    > I want to use python as a "shell like" program,
    > and execute an external program in it( such as mv, cp, tar, gnuplot)
    > I tried:


    Since you appear to be on a *nix system, a good choice is the proctools
    module in the pyNMS package.

    http://sourceforge.net/projects/pynms


    > os.execv("/bin/bash",("/usr/bin/gnuplot",'-c "gnuplot < plot.tmp"'))


    You could do this:

    import proctools
    proctools.spawnpipe("gnuplot plot.tmp")

    You can keep your existing Python 2.3 installation, as well.

    > 1. why my exec(..) command doesn't work?


    It replaces your current process.

    > 2. exec* must be with fork ?


    in this case, yes. but proctools does that for you.

    > 3. In what situation, we choose one over another ?


    The fork-and-exec is a common pattern in *nix for spawning another
    process. However, there are libraries in Python that do that for you.
    See above.


    --
    \/ \/
    (O O)
    -- --------------------oOOo~(_)~oOOo----------------------------------------
    Keith Dart <>
    public key: ID: F3D288E4
    ============================================================================
     
    Keith Dart, Dec 16, 2004
    #5
    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. C Gillespie

    spawn or fork

    C Gillespie, Dec 8, 2004, in forum: Python
    Replies:
    7
    Views:
    3,755
  2. Derek Basch
    Replies:
    2
    Views:
    1,330
    Donn Cave
    Jan 21, 2005
  3. Brendan Miller

    portable fork+exec/spawn

    Brendan Miller, May 2, 2008, in forum: Python
    Replies:
    1
    Views:
    362
    Brendan Miller
    May 2, 2008
  4. Ed Hames
    Replies:
    0
    Views:
    399
    Ed Hames
    Apr 16, 2008
  5. Edgardo Hames
    Replies:
    1
    Views:
    384
    Ed Hames
    May 6, 2008
Loading...

Share This Page