473,382 Members | 1,717 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,382 software developers and data experts.

spawn syntax + os.P_WAIT mode behavior + spawn stdout redirection

Hello,

*First question*

If the syntax of spawnl is:

spawnl(mode, path, ...)

Why does everyone write it like:

os.spawnlp(os.P_WAIT, 'cp', 'cp', 'index.html', '/dev/null')

or:

os.spawnl(os.P_WAIT, "/var/www/db/smm/smm_train", "smm_train",
"SMMTrainInput.xml")

How is the first 'cp' a path to a file? why does the desired executable have to
be named again as the first parameter?

*Second question*

I have a script test.py which calls another script sleep.py using a spawn.

--------------------------------------------------------------
#test.py
import os

os.spawnv(os.P_WAIT, "/var/www/db/cgi-bin/sleep.py", ["python", "sleep.py"])
#pid = os.spawnl(os.P_WAIT, 'sh', 'sh', '-cv', 'sleep 10; echo fark >
/tmp/test.out')
--------------------------------------------------------------

--------------------------------------------------------------
#sleep.py
import time

time.sleep(10)
--------------------------------------------------------------

I would expect that the test.py script should take 10sec to return. However it
returns immediatly. Perhaps I am calling the sleep.py script incorrectly?
Shouldn't it take 10sec to execute since the spawn mode argument is os.P_WAIT?

*Third question*

If I uncomment the second spawn call in test.py I do not get any output to
/tmp/test.out and it also returns immediatly. Can anyone tell me why?

Thank You Mighty Python Guru's,
Derek Basch

__________________________________
Do you Yahoo!?
Yahoo! Mail - now with 250MB free storage. Learn more.
http://info.mail.yahoo.com/mail_250
Jul 18 '05 #1
2 3372
Derek Basch wrote:
If the syntax of spawnl is:

spawnl(mode, path, ...)

Why does everyone write it like:

os.spawnlp(os.P_WAIT, 'cp', 'cp', 'index.html', '/dev/null')

or:

os.spawnl(os.P_WAIT, "/var/www/db/smm/smm_train", "smm_train",
"SMMTrainInput.xml")

How is the first 'cp' a path to a file? why does the desired executable have to
be named again as the first parameter?


Marginally educated guessing:

1. "cp" is a shell command, so no path is required or possible.

2. The repetition of the executable name is so that argv[0] can
have the expected contents: the name of the executable.

-Peter
Jul 18 '05 #2
Quoth Derek Basch <db****@yahoo.com>:

| *First question*
|
| If the syntax of spawnl is:
|
| spawnl(mode, path, ...)
|
| Why does everyone write it like:
|
| os.spawnlp(os.P_WAIT, 'cp', 'cp', 'index.html', '/dev/null')
|
| or:
|
| os.spawnl(os.P_WAIT, "/var/www/db/smm/smm_train", "smm_train",
| "SMMTrainInput.xml")
|
| How is the first 'cp' a path to a file?

As you may have guessed, 'cp' doesn't have to be a path because the
spawnlp() variant finds that file among a list of directories in PATH.

| why does the desired executable have to be named again as the first parameter?

Because you're supplying the "argv" argument list, which for normal
programs (i.e., not written in Python) includes argv[0] as specified
by the invoker. This would be more obvious if you consider the spawnv()
function, where these arguments are supplied as a list. You can look
at the implementation in os.py for more insight into how all this works,
particularly see the execve(2) function that is at the bottom of all this.

I was recently quite cheesed to find that the Haskell "executeFile"
function supplies its own argv[0], depriving the caller of the occasionally
useful opportunity to set this value. Python system interface functions
are generally pretty good about not watering down functionality.

| *Second question*
|
| I have a script test.py which calls another script sleep.py using a spawn.
|
| --------------------------------------------------------------
| #test.py
| import os
|
| os.spawnv(os.P_WAIT, "/var/www/db/cgi-bin/sleep.py", ["python", "sleep.py"])
| #pid = os.spawnl(os.P_WAIT, 'sh', 'sh', '-cv', 'sleep 10; echo fark >
| /tmp/test.out')
| --------------------------------------------------------------
|
| --------------------------------------------------------------
| #sleep.py
| import time
|
| time.sleep(10)
| --------------------------------------------------------------
|
| I would expect that the test.py script should take 10sec to return. However it
| returns immediatly. Perhaps I am calling the sleep.py script incorrectly?
| Shouldn't it take 10sec to execute since the spawn mode argument is os.P_WAIT?

Might want to verify that it's really executing. I suspect it isn't,
since your parameters are wrong (the file to invoke is python, not
sleep.py.) If you're writing anything important, you need to do what
you can to verify that the commands you're executing are actually
successful.

| *Third question*
|
| If I uncomment the second spawn call in test.py I do not get any output to
| /tmp/test.out and it also returns immediatly. Can anyone tell me why?

Might be a problem finding 'sh', since in this case you call spawnl(),
not spawnlp(). Just a guess. Also you ought to know that the return
from os.spawnl(os.P_WAIT, ...) will not be a pid, rather a status that
carries a little (very little) information about the problem.

Donn Cave, do**@drizzle.com
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ladvánszky Károly | last post by:
On Windows, what is the proper way to spawn an executable and redirect its stdout and stderr. Thanks for any help, Károly
3
by: Lingyun Yang | last post by:
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 <...
2
by: Nadav | last post by:
Hi, Introduction: *************** I am trying to redirect stdout to a RichEdit control, this is done by initiating a StringWriter, associated it with a StringBuilder and setting the...
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...
0
by: Antoine | last post by:
Hello list I am using Python 2.4 to invoke other programs on Windows XP, using syntax like this: os.spawnv(P_WAIT, "C:\\my.exe", ("C:\\my.exe",)) where my.exe is a console-mode program. ...
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...
0
by: Nirmal | last post by:
Hi, When I try running java through os.spawnv with P_NOWAIT, it returns the process id ex3178 true')) 19524 then I removed the command line parameter in the java command to fail ( it...
0
by: Tom Gaudasinski | last post by:
Greetings, I'm trying to redirect python's stdout to another location. The reason for this is that I'm embedding python in an application. Now, originally my code was developed for Linux and that...
2
by: gaurav kashyap | last post by:
HI all, i have two python programs as 1.py and 2.py 1.py import os import sys processID=os.spawnl(os.P_WAIT,'/usr/local/bin/python','python','/ mywork/2.py ' + 'hi') 2.py
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...

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.