开发者

How to make os.mkfifo and subprocess.Popen work together?

开发者 https://www.devze.com 2022-12-16 19:37 出处:网络
I\'m trying to redirect a patch command output using a named pipe. I tried like this: fifo = os.path.join(self.path, \'pipe\')

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消