7

我正在尝试使用 subprocess.call 在 Python 中运行外部应用程序。从我读到的内容来看,除非您调用 Popen.wait,否则 subprocess.call 不应该阻塞,但对我来说,它会阻塞直到外部应用程序退出。我该如何解决?

4

2 回答 2

5

您正在阅读错误的文档。根据他们:

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

运行 args 描述的命令。等待命令完成,然后返回 returncode 属性。

于 2013-01-09T20:56:56.363 回答
1

里面的代码subprocess实际上非常简单易读。只需查看3.32.7版本(视情况而定),您就可以知道它在做什么。

例如,call看起来像这样:

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

您可以在不调用wait. 创建一个Popen,不要调用wait它,这正是你想要的。

于 2013-01-09T21:01:53.457 回答