473,626 Members | 3,427 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2358
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********@ins ightbb.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
5275
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 time. The transcript so far is reproduced for your amusement below. To summarise: I've put up a Sudoku-solving program called Sudoku.exe. I want to call it in a php script to solve a puzzle and output the solution. It works fine with...
7
8810
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
1407
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 fork/exec if necessary. thanks.
9
1657
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 and keep running the original program. The obvious way I can think of it is using the exec* or spawn* function families. However, these are generic functions to start any external program and I was wondering if there was a special way to start a...
3
2683
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 bug or a "feature". user$ touch test.txt user$ ls -l * -rw-r--r-- 1 user user 0 Apr 18 18:30 test.txt user$ ls -l \*
2
2793
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 pid = fork()
5
2509
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 file). For each line entered, the program should fork a process that will execute the provided command using exec. i want the parent process to use wait to await the completion of the child process. I want the program to handle invalid commands by...
4
2368
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 supported on unix, but spawn is only supported on windows. The os.system call is implemented by calling the C system call, which is of course inefficient and has portability gotchas because it calls the underlying system shell (sh for unix, cmd for...
0
908
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, in fact. It's Twisted's spawnProcess API, which works on POSIX and Windows, supports supports sending and receiving, and doesn't have deadlock issues since it's event-driven. Jean-Paul
0
8205
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8644
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8370
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7206
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6126
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5579
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4208
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1817
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1516
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.