dpitch40
dpitch40

Reputation: 2691

Python subprocess.call blocking

I am trying to run an external application in Python with subprocess.call. From what I've read it subprocess.call isn't supposed to block unless you call Popen.wait, but for me it is blocking until the external application exits. How do I fix this?

Upvotes: 7

Views: 6979

Answers (2)

abarnert
abarnert

Reputation: 365737

The code in subprocess is actually pretty simple and readable. Just see the 3.3 or 2.7 version (as appropriate) and you can tell what it's doing.

For example, call looks like this:

def call(*popenargs, timeout=None, **kwargs):
    """Run command with arguments.  Wait for command to complete or
    timeout, then return the returncode attribute.

    The arguments are the same as for the Popen constructor.  Example:

    retcode = call(["ls", "-l"])
    """
    with Popen(*popenargs, **kwargs) as p:
        try:
            return p.wait(timeout=timeout)
        except:
            p.kill()
            p.wait()
            raise

You can do the same thing without calling wait. Create a Popen, don't call wait on it, and that's exactly what you want.

Upvotes: 1

g.d.d.c
g.d.d.c

Reputation: 47988

You're reading the docs wrong. According to them:

subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False)

Run the command described by args. Wait for command to complete, then return the returncode attribute.

Upvotes: 5

Related Questions