How to make os.mkfifo and subprocess.Popen work together?
I'm trying to redirect a patch command output using a named pipe. I tried like this:
fifo = os.path.join(self.path, 'pipe')
os.mkfifo(fifo)
op = os.popen('cat '+ fifo)
proc = Popen(['patch', current_keyframe, '--input='+fpath, '--output='+fifo], stdin=PIPE, stdout=PIPE)
os.unlink(fifo)
print op开发者_开发问答.read()
But my script stops at Popen() call just like patch command didn't completed. How I can make it work properly?
You aren't waiting for the patch command to finish before you read from the fifo. Replace the subprocess.Popen()
call with subprocess.call()
, and remove the stdin/stdout redirections you aren't using. Also, use open(fifo)
to read from the fifo, not os.popen('cat ' + fifo)
.
You realize, I hope, that you can avoid the FIFO entirely? After p = Popen(['patch', '--input', fpath], stdout=PIPE)
, you can just read patch's output from p.stdout.
精彩评论