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

L

Lingyun Yang

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
 
P

Peter Hansen

Lingyun said:
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
 
B

Binu K S

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

Keith Dart

Lingyun said:
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 <[email protected]>
public key: ID: F3D288E4
============================================================================
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top