473,412 Members | 2,994 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

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

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
3 2345
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Greg Chapman | last post by:
I am at my wit's end trying to get information out of Streamline.net's support dept about my problem. They reply quickly enough, but seem to try and give out the least possible amount of info each...
7
by: C Gillespie | last post by:
Dear All, I have a function def printHello(): fp = open('file','w') fp.write('hello') fp.close() I would like to call that function using spawn or fork. My questions are:
3
by: Sinan Nalkaya | last post by:
hi, i am using os.spawn function, it works well but i need a flag that allows function return the process id with exit/error code, is there any or how can i do it, i can replace spawn with...
9
by: Daniel Nogradi | last post by:
Is it possible to pass a python object to a python program as argument? In my program I would like to start executing an other python program and don't wait until that finishes, only launch it...
3
by: jeremyfee | last post by:
The spawn* and exec* functions appear to escape asterisks, I'm guessing all shell characters too, before the spawn/exec'ed process sees them. Is there a way around this? Not sure if this is a...
2
by: Rafael Giannetti Viotti | last post by:
Hi, I am working with the subprocess.py module in Python 2.4.4 and I am confused about it's functionality. It uses the standard pipe-fork-exec method to start a subprocess: # create pipes ...
5
by: Jatek | last post by:
Hi i have learned how fork.pl and exec.pl work as a starting point,but i want to create a program called shell.pl that will repeatedly read a line of user inpu until the user enters CTRL-D (end of...
4
by: Brendan Miller | last post by:
I want to spawn a child process based on an external executable that I have the path for. I then want to wait on that executable, and capture it's output. In the os module, fork is only...
0
by: Jean-Paul Calderone | last post by:
On Fri, 02 May 2008 06:30:03 -0500, Nick Craig-Wood <nick@craig-wood.comwrote: I haven't read all of this thread, so excuse me if this is out of place. There is a good cross-platform solution,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.