Proper binding of Popen subprocesses

i has the following construction:

os.mkfifo('pipe.tmp') enc = Popen(['encoder', '-i', 'pipe.tmp']) cap = Popen(['capture', '-f', 'pipe.tmp']) 

here cap is a process that is usually written to a file (specified by -f ), but I can make it pop the data to the screen by providing /dev/stdout as the output file. likewise, enc waits for reading from a file-like object, and I can get it to be read from the channel by providing - as input. so instead of using the named pipe in os, I thought that a special file might not be needed, I can use an unnamed pipe like this.

 cap = Popen(['capture', '-f', '/dev/stdout'], stdout=PIPE) enc = Popen(['encoder', '-i', '-'], stdin=cap.stdout) cap.stdout.close() 

(also pay attention to the reversal of the spawning order). I like it better because a temporary file seems unnecessary, but it bothers me a bit whether this construct will bind processes as I expect.

  • is it /dev/stdout that cap says is different from the actual stdout in the OS? that is, with the input channel - in enc will I get a clean data channel between these two processes, even if other processes communicate with / dev / stdout in the OS?
  • Will there be any significant differences in lock / queue behavior? I think in my first example, the named pipe will be buffered 4096 bytes and will block at both ends if cap / enc not writing / reading fast enough, but correct me if I'm wrong.
  • Is any special order of spawning or termination required or any other errors that I should be aware of?
+7
source share
1 answer
  • / dev / stdout gives you stdout for the current process, so you must be fine to use this. (there is nothing "global" in / dev / stdout)
  • The fifo size in the first example depends on your system configuration (I'm not sure how to change this). But subprocess.Popen allows you to determine the size of the buffer for I / O, so you can configure it. Update: a bit more research, I found that there is a 64 kB limit for pipes that are not affected by the bufsize argument. Not sure how to get around this (except to read more often and handle buffering manually)
  • For your second example, it looks like you need to start in the order you specify, since you need cap.stdout to be available before running enc. Alternatively, you can disable two processes and manually establish a connection between them.
+1
source

All Articles