如何使 os.mkfifo 和 subprocess.Popen 一起工作?

发布于 2024-08-18 07:41:08 字数 385 浏览 1 评论 0原文

我正在尝试使用命名管道重定向 patch 命令输出。我尝试这样:

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()

但我的脚本在 Popen() 调用处停止,就像 patch 命令未完成一样。我怎样才能让它正常工作?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浸婚纱 2024-08-25 07:41:08

在从 fifo 读取数据之前,您无需等待 patch 命令完成。将 subprocess.Popen() 调用替换为 subprocess.call(),并删除不使用的 stdin/stdout 重定向。另外,请使用 open(fifo) 从 fifo 读取数据,而不是 os.popen('cat ' + fifo)

我希望您意识到可以完全避免 FIFO 吗?在 p = Popen(['patch', '--input', fpath], stdout=PIPE) 之后,您可以从 p.stdout 读取 patch 的输出。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文