Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > spawn* or exec* and fork, what should I use and how ?

Reply
Thread Tools

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

 
 
Lingyun Yang
Guest
Posts: n/a
 
      12-16-2004
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
 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      12-16-2004
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
 
Reply With Quote
 
 
 
 
Binu K S
Guest
Posts: n/a
 
      12-16-2004
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 <(E-Mail Removed)> 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
>

 
Reply With Quote
 
Lingyun Yang
Guest
Posts: n/a
 
      12-16-2004
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/...ubprocess.html
 
Reply With Quote
 
Keith Dart
Guest
Posts: n/a
 
      12-16-2004
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 <(E-Mail Removed)>
public key: ID: F3D288E4
================================================== ==========================
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
should I use java.awt.* only or also use javax.swing in applets? steveh44 Java 12 05-06-2011 05:06 PM
Builtn super() function. How to use it with multiple inheritance? Andwhy should I use it at all? Lacrima Python 43 08-02-2010 09:28 PM
I use IE right now--Should I use Firefox or Mozilla? BOB Firefox 10 04-16-2006 08:23 PM
What the FAQs should and should not contain Josef 'Jupp' SCHUGT Ruby 0 08-19-2005 01:46 PM



Advertisments