Asychronous execution *with* return codes?

Discussion in 'Python' started by utabintarbo, Oct 5, 2006.

  1. utabintarbo

    utabintarbo Guest

    I hope I have not overlooked a solution already posted, but I seem to
    be unable to suss out a way to achieve both multiple console-less
    executions of a given (console) application and gathering the return
    code from the application.

    What I have found:
    <code>
    import subprocess

    # gives back return code, but does not run asynchronously
    retcode = subprocess.call([app, lstArgs])
    retcode = subprocess.Popen([app] + lstArgs).wait()
    # runs the app async, but only returns the pid (no return code)
    pid = subprocess.Popen([app] + lstArgs).pid
    </code>

    Is there some magic elixir which will get me both?

    TIA
    utabintarbo, Oct 5, 2006
    #1
    1. Advertising

  2. utabintarbo

    MonkeeSage Guest

    MonkeeSage, Oct 5, 2006
    #2
    1. Advertising

  3. utabintarbo

    utabintarbo Guest

    MonkeeSage wrote:
    > utabintarbo wrote:
    > > pid = subprocess.Popen([app] + lstArgs).pid

    >
    > Check out the poll() method and the returncode attribute:
    > http://docs.python.org/lib/node533.html
    >


    Thanks for the reply.

    If I understand your meaning, I should do something like this (given I
    wish to run an app against several arguments [my use case]):

    for lstArgs in pileOflstArgs:
    uniqueProcessID = subprocess.Popen([app] + lstArgs)
    pid = uniqueProcessID.pid
    retcode = uniqueProcessID.poll()
    # increment uniqueProcessID
    ....

    If so, how do I handle the poll() on long-running processes? Run a
    bunch and then start a check loop? Am I asking too many questions?
    utabintarbo, Oct 5, 2006
    #3
  4. utabintarbo

    Justin Guest

    If you're on a POSIX system, you could use the usual fork/exec/wait:

    import os
    for lstArgs in pileOflstArgs:
    pid = os.fork()
    if not pid:
    os.execv( app, lstArgs )

    for i in range(len(pileOflstArgs)):
    pid, status = os.wait()

    Of couse, os.wait() will block until a child exits. Look at the docs
    for the status code it returns, though, as it's not just the return
    value of the process.

    On Oct 5, 7:43 am, "utabintarbo" <> wrote:
    > MonkeeSage wrote:
    > > utabintarbo wrote:
    > > > pid = subprocess.Popen([app] + lstArgs).pid

    >
    > > Check out the poll() method and the returncode attribute:
    > >http://docs.python.org/lib/node533.htmlThanks for the reply.

    >
    > If I understand your meaning, I should do something like this (given I
    > wish to run an app against several arguments [my use case]):
    >
    > for lstArgs in pileOflstArgs:
    > uniqueProcessID = subprocess.Popen([app] + lstArgs)
    > pid = uniqueProcessID.pid
    > retcode = uniqueProcessID.poll()
    > # increment uniqueProcessID
    > ....
    >
    > If so, how do I handle the poll() on long-running processes? Run a
    > bunch and then start a check loop? Am I asking too many questions?
    Justin, Oct 5, 2006
    #4
  5. utabintarbo

    utabintarbo Guest

    Justin wrote:
    > If you're on a POSIX system, you could use the usual fork/exec/wait:
    >

    Sorry. Win32. We are only allowed spoons - no sharp objects. :p
    utabintarbo, Oct 5, 2006
    #5
  6. At Thursday 5/10/2006 09:33, utabintarbo wrote:

    ># gives back return code, but does not run asynchronously
    >retcode = subprocess.call([app, lstArgs])
    >retcode = subprocess.Popen([app] + lstArgs).wait()
    ># runs the app async, but only returns the pid (no return code)
    >pid = subprocess.Popen([app] + lstArgs).pid
    ></code>
    >
    >Is there some magic elixir which will get me both?


    Use the async way, and then, os.waitpid()



    Gabriel Genellina
    Softlab SRL





    __________________________________________________
    Preguntá. Respondé. Descubrí.
    Todo lo que querías saber, y lo que ni imaginabas,
    está en Yahoo! Respuestas (Beta).
    ¡Probalo ya!
    http://www.yahoo.com.ar/respuestas
    Gabriel Genellina, Oct 5, 2006
    #6
  7. In message <>,
    utabintarbo wrote:

    > Justin wrote:
    >> If you're on a POSIX system, you could use the usual fork/exec/wait:
    >>

    > Sorry. Win32. We are only allowed spoons - no sharp objects. :p


    How about installing Cygwin, then, and running under that?
    Lawrence D'Oliveiro, Oct 9, 2006
    #7
  8. utabintarbo wrote:

    > If so, how do I handle the poll() on long-running processes? Run a
    > bunch and then start a check loop?


    or use a thread to keep track of each external process.

    </F>
    Fredrik Lundh, Oct 9, 2006
    #8
  9. utabintarbo

    utabintarbo Guest

    Fredrik Lundh wrote:
    > utabintarbo wrote:
    >
    > > If so, how do I handle the poll() on long-running processes? Run a
    > > bunch and then start a check loop?

    >
    > or use a thread to keep track of each external process.
    >
    > </F>


    This sounds most promising. Might you have a code snippet (or link to
    same) illustrating this?
    utabintarbo, Oct 11, 2006
    #9
    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. Ruth
    Replies:
    1
    Views:
    1,394
    Tim Hubberstey
    Jun 26, 2003
  2. yali
    Replies:
    2
    Views:
    718
    anupam
    Aug 31, 2004
  3. Frank Mamone
    Replies:
    2
    Views:
    463
    Frank Mamone
    Sep 14, 2004
  4. SenthilVel

    how to use asychronous calls

    SenthilVel, May 15, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    399
    SenthilVel
    May 15, 2006
  5. Replies:
    2
    Views:
    2,793
    Malcolm
    Aug 20, 2005
Loading...

Share This Page