子流程Popen和call有什么区别(我该如何使用它们)?

问题:子流程Popen和call有什么区别(我该如何使用它们)?

我想从Python调用一个外部程序。我已经使用过Popen()并且call()做到了。

两者有什么区别?

我的特定目标是从Python运行以下命令。我不确定重定向如何工作。

./my_script.sh > output

我阅读了文档,并说它call()是便利功能或快捷功能。我们使用call()代替会失去任何功能Popen()吗?

I want to call an external program from Python. I have used both Popen() and call() to do that.

What’s the difference between the two?

My specific goal is to run the following command from Python. I am not sure how redirects work.

./my_script.sh > output

I read the documentation and it says that call() is a convenience function or a shortcut function. Do we lose any power by using call() instead of Popen()?


回答 0

重定向有两种方法。两者都适用于subprocess.Popensubprocess.call

  1. 设置关键字参数shell = Trueexecutable = /path/to/the/shell并在那里指定命令。

  2. 由于您只是将输出重定向到文件,因此请设置关键字参数

    stdout = an_open_writeable_file_object

    对象指向output文件的位置。

subprocess.Popensubprocess.call

Popen不会阻塞,允许您在进程运行时与之进行交互,或者继续进行Python程序中的其他操作。调用Popen返回一个Popen对象。

call 阻止。它支持与Popen构造函数相同的所有参数,因此您仍可以设置进程的输出,环境变量等,脚本将等待程序完成,并call返回表示进程退出状态的代码。

returncode = call(*args, **kwargs) 

与通话基本相同

returncode = Popen(*args, **kwargs).wait()

call只是一种便利功能。它在CPython的实现是在subprocess.py

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

如您所见,它周围是薄薄的包装纸Popen

There are two ways to do the redirect. Both apply to either subprocess.Popen or subprocess.call.

  1. Set the keyword argument shell = True or executable = /path/to/the/shell and specify the command just as you have it there.

  2. Since you’re just redirecting the output to a file, set the keyword argument

    stdout = an_open_writeable_file_object
    

    where the object points to the output file.

subprocess.Popen is more general than subprocess.call.

Popen doesn’t block, allowing you to interact with the process while it’s running, or continue with other things in your Python program. The call to Popen returns a Popen object.

call does block. While it supports all the same arguments as the Popen constructor, so you can still set the process’ output, environmental variables, etc., your script waits for the program to complete, and call returns a code representing the process’ exit status.

returncode = call(*args, **kwargs) 

is basically the same as calling

returncode = Popen(*args, **kwargs).wait()

call is just a convenience function. It’s implementation in CPython is in subprocess.py:

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

As you can see, it’s a thin wrapper around Popen.


回答 1

另一个答案很完整,但这是一个经验法则:

  • call 正在阻止:

    call('notepad.exe')
    print('hello')  # only executed when notepad is closed
    
  • Popen 是非阻塞的:

    Popen('notepad.exe')
    print('hello')  # immediately executed
    

The other answer is very complete, but here is a rule of thumb:

  • call is blocking:

    call('notepad.exe')
    print('hello')  # only executed when notepad is closed
    
  • Popen is non-blocking:

    Popen('notepad.exe')
    print('hello')  # immediately executed