简体   繁体   中英

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM