• <legend id='26O7S'><style id='26O7S'><dir id='26O7S'><q id='26O7S'></q></dir></style></legend>

      <i id='26O7S'><tr id='26O7S'><dt id='26O7S'><q id='26O7S'><span id='26O7S'><b id='26O7S'><form id='26O7S'><ins id='26O7S'></ins><ul id='26O7S'></ul><sub id='26O7S'></sub></form><legend id='26O7S'></legend><bdo id='26O7S'><pre id='26O7S'><center id='26O7S'></center></pre></bdo></b><th id='26O7S'></th></span></q></dt></tr></i><div id='26O7S'><tfoot id='26O7S'></tfoot><dl id='26O7S'><fieldset id='26O7S'></fieldset></dl></div>
    1. <small id='26O7S'></small><noframes id='26O7S'>

      <tfoot id='26O7S'></tfoot>
        • <bdo id='26O7S'></bdo><ul id='26O7S'></ul>

        解释来自 Python 子流程模块的示例管道

        时间:2023-07-22

          <small id='qg48h'></small><noframes id='qg48h'>

            <legend id='qg48h'><style id='qg48h'><dir id='qg48h'><q id='qg48h'></q></dir></style></legend>
            <i id='qg48h'><tr id='qg48h'><dt id='qg48h'><q id='qg48h'><span id='qg48h'><b id='qg48h'><form id='qg48h'><ins id='qg48h'></ins><ul id='qg48h'></ul><sub id='qg48h'></sub></form><legend id='qg48h'></legend><bdo id='qg48h'><pre id='qg48h'><center id='qg48h'></center></pre></bdo></b><th id='qg48h'></th></span></q></dt></tr></i><div id='qg48h'><tfoot id='qg48h'></tfoot><dl id='qg48h'><fieldset id='qg48h'></fieldset></dl></div>

                  <tbody id='qg48h'></tbody>
              1. <tfoot id='qg48h'></tfoot>

                • <bdo id='qg48h'></bdo><ul id='qg48h'></ul>

                • 本文介绍了解释来自 Python 子流程模块的示例管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  17.1.4.2:替换 shell 管道部分python subprocess 模块说要替换

                  Section 17.1.4.2: Replacing shell pipeline of the python subprocess module says to replace

                  output=`dmesg | grep hda`
                  

                  p1 = Popen(["dmesg"], stdout=PIPE)
                  p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
                  p1.stdout.close()  # Allow p1 to receive a SIGPIPE if p2 exits.
                  output = p2.communicate()[0]
                  

                  第三行的注释解释了为什么调用 close 函数,但没有解释为什么它有意义.对我来说不是.在调用通信方法之前不关闭 p1.stdout 会阻止 any 输出通过管道发送吗?(显然不会,我尝试运行代码并且运行良好).为什么需要调用close来让p1接收SIGPIPE?什么样的关闭是不关闭的?究竟是什么,它正在关闭?

                  The comment to the third line explains why the close function is being called, but not why it makes sense. It doesn't, to me. Will not closing p1.stdout before the communicate method is called prevent any output from being sent through the pipe? (Obviously it won't, I've tried to run the code and it runs fine). Why is it necessary to call close to make p1 receive SIGPIPE? What kind of close is it that doesn't close? What, exactly, is it closing?

                  请考虑这是一个学术问题,除了更好地理解这些事情之外,我并没有试图完成任何事情.

                  Please consider this an academic question, I'm not trying to accomplish anything except understanding these things better.

                  推荐答案

                  你正在关闭 parent 进程中的 p1.stdout,因此 dmesg 作为唯一的进程该文件描述符打开.如果您不这样做,即使 dmesg 关闭了它的标准输出,您仍然会打开它,并且不会生成 SIGPIPE.(操作系统基本上保持一个引用计数,并在它达到零时生成 SIGPIPE.如果你不关闭文件,你会阻止它达到零.)

                  You are closing p1.stdout in the parent process, thus leaving dmesg as the only process with that file descriptor open. If you didn't do this, even when dmesg closed its stdout, you would still have it open, and a SIGPIPE would not be generated. (The OS basically keeps a reference count, and generates SIGPIPE when it hits zero. If you don't close the file, you prevent it from ever reaching zero.)

                  这篇关于解释来自 Python 子流程模块的示例管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:对消息进行 grepping 时,bufsize 必须是整数错误 下一篇:在 subprocess.call 中使用大于运算符

                  相关文章

                      • <bdo id='akhGR'></bdo><ul id='akhGR'></ul>
                    1. <tfoot id='akhGR'></tfoot>

                      <legend id='akhGR'><style id='akhGR'><dir id='akhGR'><q id='akhGR'></q></dir></style></legend>
                      <i id='akhGR'><tr id='akhGR'><dt id='akhGR'><q id='akhGR'><span id='akhGR'><b id='akhGR'><form id='akhGR'><ins id='akhGR'></ins><ul id='akhGR'></ul><sub id='akhGR'></sub></form><legend id='akhGR'></legend><bdo id='akhGR'><pre id='akhGR'><center id='akhGR'></center></pre></bdo></b><th id='akhGR'></th></span></q></dt></tr></i><div id='akhGR'><tfoot id='akhGR'></tfoot><dl id='akhGR'><fieldset id='akhGR'></fieldset></dl></div>

                      <small id='akhGR'></small><noframes id='akhGR'>