<tfoot id='7IkMr'></tfoot>
          <bdo id='7IkMr'></bdo><ul id='7IkMr'></ul>

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

        <small id='7IkMr'></small><noframes id='7IkMr'>

        <legend id='7IkMr'><style id='7IkMr'><dir id='7IkMr'><q id='7IkMr'></q></dir></style></legend>

      2. subprocess.call() 和 subprocess.Popen() 之间有什么区别使 PIPE 对前者的安全性

        时间:2023-07-21
        <i id='SCKh1'><tr id='SCKh1'><dt id='SCKh1'><q id='SCKh1'><span id='SCKh1'><b id='SCKh1'><form id='SCKh1'><ins id='SCKh1'></ins><ul id='SCKh1'></ul><sub id='SCKh1'></sub></form><legend id='SCKh1'></legend><bdo id='SCKh1'><pre id='SCKh1'><center id='SCKh1'></center></pre></bdo></b><th id='SCKh1'></th></span></q></dt></tr></i><div id='SCKh1'><tfoot id='SCKh1'></tfoot><dl id='SCKh1'><fieldset id='SCKh1'></fieldset></dl></div>

        <tfoot id='SCKh1'></tfoot>

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

                <tbody id='SCKh1'></tbody>
              • <bdo id='SCKh1'></bdo><ul id='SCKh1'></ul>

                  <legend id='SCKh1'><style id='SCKh1'><dir id='SCKh1'><q id='SCKh1'></q></dir></style></legend>
                  本文介绍了subprocess.call() 和 subprocess.Popen() 之间有什么区别使 PIPE 对前者的安全性降低?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我已经查看了他们两个的文档.

                  JF 在这里的评论提示了这个问题:

                  不管你的 Python 版本是什么——管道缓冲区(看图片)在你的 Python 进程之外.Python 3 不使用 C stdio,但它只影响内部缓冲.当内部缓冲区被刷新时,数据进入管道.如果 command2(你的父 Python 程序)没有从管道中读取,那么 command1(子进程,例如,由 call() 启动)将管道缓冲区满后立即挂起 (pipe_size = fcntl(p.stdout, F_GETPIPE_SZ) ~65K 在我的 Linux 机器上(最大值是 /proc/sys/fs/pipe-max-size ~1M)).

                  如果您稍后从管道中读取,您可以使用 stdout=PIPE,例如,使用 Popen.communicate() 方法.您也可以直接从process.stdout(代表管道的文件对象)读取.

                  I've had a look at the documentation for both of them.

                  This question is prompted by J.F.'s comment here: Retrieving the output of subprocess.call()

                  The current Python documentation for subprocess.call() says the following about using PIPE for subprocess.call():

                  Note Do not use stdout=PIPE or stderr=PIPE with this function. The child process will block if it generates enough output to a pipe to fill up the OS pipe buffer as the pipes are not being read from.

                  Python 2.7 subprocess.call():

                  Note Do not use stdout=PIPE or stderr=PIPE with this function as that can deadlock based on the child process output volume. Use Popen with the communicate() method when you need pipes.

                  Python 2.6 includes no such warnings.

                  Also, the subprocess.call() and subprocess.check_call() don't seem to have a way to access their output, except for using stdout=PIPE with communicate():

                  https://docs.python.org/2.6/library/subprocess.html#convenience-functions

                  Note that if you want to send data to the process’s stdin, you need to create the Popen object with stdin=PIPE. Similarly, to get anything other than None in the result tuple, you need to give stdout=PIPE and/or stderr=PIPE too.

                  https://docs.python.org/2.6/library/subprocess.html#subprocess.Popen.communicate

                  What difference between subprocess.call() and subprocess.Popen() makes PIPE less secure for subprocess.call()?

                  More Specific: Why does subprocess.call() "deadlock based on the child process output volume.", and not Popen()?

                  解决方案

                  call() is just Popen().wait() (± error handling).

                  You should not use stdout=PIPE with call() because it does not read from the pipe and therefore the child process will hang as soon as it fills the corresponding OS pipe buffer. Here's a picture that shows how data flows in command1 | command2 shell pipeline:

                  It does not matter what your Python version is -- the pipe buffer (look at the picture) is outside of your Python process. Python 3 does not use C stdio but it affects only the internal buffering. When the internal buffer is flushed the data goes into the pipe. If command2 (your parent Python program) does not read from the pipe then command1 (the child process e.g., started by call()) will hang as soon as the pipe buffer is full (pipe_size = fcntl(p.stdout, F_GETPIPE_SZ) ~65K on my Linux box (max value is /proc/sys/fs/pipe-max-size ~1M)).

                  You may use stdout=PIPE if you read from the pipe later e.g., using Popen.communicate() method. You could also read from process.stdout (the file object that represents the pipe) directly.

                  这篇关于subprocess.call() 和 subprocess.Popen() 之间有什么区别使 PIPE 对前者的安全性降低?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Python 子进程模块比命令慢得多(已弃用) 下一篇:Python:非阻塞+非失效进程

                  相关文章

                  <tfoot id='NDwIn'></tfoot>

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

                  2. <legend id='NDwIn'><style id='NDwIn'><dir id='NDwIn'><q id='NDwIn'></q></dir></style></legend>