0

When using subprocess.Popen (or something which is using Popen internally and forwarding all parameters like check_output) to start a process where the output, I would've let stdout and stderr point to /dev/null (or the Windows equivalent) as described in How to hide output of subprocess in Python 2.7.

Now I just came across a big Python project where both these parameters were simply set to PIPE. This seems to be no problem here because there is need to wait for the end of the process and in fact the process is always running until it's killed by the Python program (i.e. no .wait()/.poll()/.communicate() call).

Now I wonder if that's a good idea and what is happening under the hood. A pipe is opened but nobody is reading from it and therefore nothing is written there (though non-blocking I guess)? Or a pipe is opened and the process is writing there and it's filling up some memory location?

Most importantly: Is this a good idea?

Dakkaron
  • 5,930
  • 2
  • 36
  • 51
phk
  • 2,002
  • 1
  • 29
  • 54
  • 1
    not a good idea. pipes block, if the buffer is full but never read. – Daniel Oct 23 '16 at 12:22
  • @Daniel https://docs.python.org/2/library/subprocess.html#subprocess.Popen.wait mentions that this is a problem when using `.wait()` but this is not the case here. – phk Oct 23 '16 at 12:25
  • 1
    How could we tell if the process is writing to the pipe or not? If the process is writing to stdout it will certainly be writing to the pipe. If it is not then you do not need to redirect because there is nothing to hide. – Stop harming Monica Oct 23 '16 at 12:29
  • @Goyo: The processes started by the Python program do write output, not necessarily a lot, but they do. – phk Oct 23 '16 at 12:30
  • @phk Then they will be writing to whatever object you redirected stdout to. – Stop harming Monica Oct 23 '16 at 12:31
  • @Goyo As mentioned in the text it's set to `subprocess.PIPE`. – phk Oct 23 '16 at 12:34
  • 1
    Even if you don't call `wait` or `communicate`, the data is received by the `Popen` object. In fact, you can do `out, err = p.communicate()` after the process `p` finishes and you'll get the output string. – zvone Oct 23 '16 at 12:43
  • @phk I know, just trying to generalize. A pipe will be opened and the output will be written to it so you can read from it later, no matter whether you actually read or not. – Stop harming Monica Oct 23 '16 at 12:44
  • @zvone Thank you BTW for the answer, my pull request for the project has been accepted which I guess also shows that you were right. If you want you can make your comment an answer. – phk Dec 28 '16 at 00:54

0 Answers0