0

On python 2.7, I used subprocess.call(my_cmd) when running a shell command.

However, I needed to check outputs of those commands, so I replaced them with subprocess.Popen.communicate().

    command_line_process = subprocess.Popen(my_cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    process_stdout, process_stderr = command_line_process.communicate()
    logging.info(process_stdout)
    logging.info(process_stderr)

Is there a difference between the two methods other than the latter one can print outputs?

I wonder whether it'd be okay to replace all subprocess.call() with subprocess.Popen.communicate().

yoon
  • 1,177
  • 2
  • 15
  • 28

2 Answers2

1

If you check inside the source code, which you have on your system, you'll see that subprocess.call is implemented by calling subprocess.Popen.wait. Virtually everything in that module is actually implemented in terms of subprocess.Popen.

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
1

Like the documentation already tells you, you want to avoid Popen whenever you can.

The subprocess.check_output() function in Python 2.7 lets you retrieve the output from the subprocess, but otherwise works basically like check_call() (which in turn differs from call only in that it will raise an exception if the subprocess fails, which is something you usually want and need).

The case for Popen is that it enables you to build new high-level functions like these if you need to (like also then subprocess.run() in Python 3.5+ which is rather more versatile, and basically subsumes the functionality of all the above three). They all use Popen under the hood, but it is finicky and requires you to take care of several details related to managing the subprocess object if you use it directly; those higher-level functions already do those things for you.

Common cases where you do need Popen is if you want the subprocess to run in parallel with your main Python script. There is no simple library function which does this for you.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • For much more on the topic, see also https://stackoverflow.com/questions/4256107/running-bash-commands-in-python/51950538#51950538 – tripleee May 11 '21 at 04:42