By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,693 Members | 1,646 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,693 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
Jul 18 '05 #2

P: n/a
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 <ji********@insightbb.com> 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

Jul 18 '05 #3

P: n/a
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
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.