How does subprocess capture the output of a process with a pipe?

Question:

What exactly does subprocess do to capture the output of the of the thing being run?

  • Is it using some OS hook to direct the output into a shared ram space?
  • Does it direct the process to write to a file on disk that it then reads? Where is the file?
  • Network Socket?
  • Does it do something else?
proc = subprocess.Popen(cmd, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)

Windows 10

Python 3.7

Asked By: Davep1553

||

Answers:

you could check its source code, it’s just a fancy wrapper around Create Process, for windows which accepts input and output and error buffers, which are typically buffers wrapped as python TextIO buffer. (think of it like StringIO or BytesIO buffers)

on linux since there is no CreateProcess function, this is changed to a fork then exec after rewiring the standard input and output buffers, which you can check its source code

Answered By: Ahmed AEK
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.