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

Discussion in 'Python' started by Derek Basch, Jan 21, 2005.

  1. Derek Basch

    Derek Basch Guest

    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
    Derek Basch, Jan 21, 2005
    #1
    1. Advertising

  2. Derek Basch

    Peter Hansen Guest

    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
    Peter Hansen, Jan 21, 2005
    #2
    1. Advertising

  3. Derek Basch

    Donn Cave Guest

    Quoth Derek Basch <>:

    | *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,
    Donn Cave, Jan 21, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Donn Cave
    Replies:
    1
    Views:
    541
    TH Lim
    Jul 10, 2003
  2. Ladvánszky Károly

    Question: spawn and redirection

    Ladvánszky Károly, Feb 13, 2004, in forum: Python
    Replies:
    6
    Views:
    663
    Peter Hansen
    Feb 13, 2004
  3. Jelle Feringa // EZCT / Paris

    os.spawn & stdOUT trouble

    Jelle Feringa // EZCT / Paris, Jan 11, 2005, in forum: Python
    Replies:
    0
    Views:
    323
    Jelle Feringa // EZCT / Paris
    Jan 11, 2005
  4. Ed Hames
    Replies:
    0
    Views:
    363
    Ed Hames
    Apr 16, 2008
  5. Edgardo Hames
    Replies:
    1
    Views:
    333
    Ed Hames
    May 6, 2008
Loading...

Share This Page