subprocess wait() waits forever, but os.system returns

Discussion in 'Python' started by grayaii, May 7, 2008.

  1. grayaii

    grayaii Guest

    There are so many threads on this subject, but I ran across a
    situation on Windows that I can't figure out.

    I'm trying to run this little command-line exe and when I launch like
    this, it hangs:

    >>> import subprocess
    >>> command = r'c:\mydir\foo.exe'
    >>> run = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stdin=None, stderr=subprocess.PIPE, env=os.environ, universal_newlines=True)
    >>> returncode = run.wait() ## HANGS HERE ##


    I can run this exe manually via the command prompt and it returns
    after a few seconds, but more importantly when I run it as follows it
    works fine:

    >>> import os
    >>> command = r'c:\mydir\foo.exe'
    >>> os.system(command) ## WORKS FINE! ##


    Unfortunately I don't know too much about the exe (well, I do know
    that it spits out some stdout that I collect, but I don't know the
    exe's source code.)

    I can't figure out why the subprocess module is having a hard time
    with this particular exe. I've tried so many different permutations
    of subprocess.Popen and they all hang on this exe. Even if try to do
    the usual (pseudo code):
    while(returncode is None):
    returncode = run.poll()
    time.sleep(1)
    blah blah blah

    returncode is always None... In other words, it's hung. I can't
    figure out why os.system works fine, but subprocess.Popen thinks the
    process hasn't finished.

    Any ideas would be greatly appreciated. I'm all ears.
     
    grayaii, May 7, 2008
    #1
    1. Advertising

  2. grayaii schrieb:
    > There are so many threads on this subject, but I ran across a
    > situation on Windows that I can't figure out.
    >
    > I'm trying to run this little command-line exe and when I launch like
    > this, it hangs:
    >
    >>>> import subprocess
    >>>> command = r'c:\mydir\foo.exe'
    >>>> run = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stdin=None, stderr=subprocess.PIPE, env=os.environ, universal_newlines=True)
    >>>> returncode = run.wait() ## HANGS HERE ##


    The code blocks because you aren't reading from stdout and stderr. Use
    the communicate() method instead of wait().

    Christian
     
    Christian Heimes, May 7, 2008
    #2
    1. Advertising

  3. grayaii schrieb:
    > There are so many threads on this subject, but I ran across a
    > situation on Windows that I can't figure out.
    >
    > I'm trying to run this little command-line exe and when I launch like
    > this, it hangs:
    >
    >>>> import subprocess
    >>>> command = r'c:\mydir\foo.exe'
    >>>> run = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stdin=None, stderr=subprocess.PIPE, env=os.environ, universal_newlines=True)
    >>>> returncode = run.wait() ## HANGS HERE ##


    The code blocks because you aren't reading from stdout and stderr. Use
    the communicate() method instead of wait().

    Christian
     
    Christian Heimes, May 7, 2008
    #3
  4. grayaii

    grayaii Guest

    Awesome! That worked!
    And your comment just led me down another exploration path on why the
    following doesn't work:
    ---------------------
    while(returncode is None):
    returncode = run.poll()
    time.sleep(1)

    out = run.stdout.readlines()
    err = run.stderr.readlines()
    ---------------------
    Nowhere in the loop am I reading stdout or err.
    I'm only reading it after the loop has finished, and when running the
    exe, returncode is *always* None.
    Now I have to figure out a way to read it within the loop without
    blocking the read... on Windows...

    Thinking out loud: Perhaps it would be better to put the subprocess
    commands in a separate thread so if my process hangs, I can kill it
    after a given time. Various threads in this forum suggested that
    method, so perhaps I should do it that way...
     
    grayaii, May 7, 2008
    #4
  5. grayaii schrieb:
    > Awesome! That worked!
    > And your comment just led me down another exploration path on why the
    > following doesn't work:
    > ---------------------
    > while(returncode is None):
    > returncode = run.poll()
    > time.sleep(1)
    >
    > out = run.stdout.readlines()
    > err = run.stderr.readlines()
    > ---------------------
    > Nowhere in the loop am I reading stdout or err.
    > I'm only reading it after the loop has finished, and when running the
    > exe, returncode is *always* None.
    > Now I have to figure out a way to read it within the loop without
    > blocking the read... on Windows...


    the subprocess has already a canonical way:

    out, err = run.communicate()

    Your code may still block if the stderr buffer is full during the
    stdout.readlines() call.

    Christian
     
    Christian Heimes, May 7, 2008
    #5
    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. GrantMagic

    Timer while user waits

    GrantMagic, Sep 9, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    359
    Rutger Smit
    Sep 9, 2004
  2. Replies:
    2
    Views:
    8,727
    Sebastian Scheid
    Aug 25, 2005
  3. Imran
    Replies:
    1
    Views:
    893
    Roedy Green
    Oct 15, 2005
  4. Replies:
    13
    Views:
    777
    Donn Cave
    Mar 6, 2007
  5. Mithil
    Replies:
    2
    Views:
    332
    Tom Hawtin
    May 29, 2007
Loading...

Share This Page