• <small id='NTbwI'></small><noframes id='NTbwI'>

  • <tfoot id='NTbwI'></tfoot>

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

        子进程Popen和通信后关闭所有文件的正确方法

        时间:2023-09-03
              <tbody id='oFF9b'></tbody>

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

              1. <i id='oFF9b'><tr id='oFF9b'><dt id='oFF9b'><q id='oFF9b'><span id='oFF9b'><b id='oFF9b'><form id='oFF9b'><ins id='oFF9b'></ins><ul id='oFF9b'></ul><sub id='oFF9b'></sub></form><legend id='oFF9b'></legend><bdo id='oFF9b'><pre id='oFF9b'><center id='oFF9b'></center></pre></bdo></b><th id='oFF9b'></th></span></q></dt></tr></i><div id='oFF9b'><tfoot id='oFF9b'></tfoot><dl id='oFF9b'><fieldset id='oFF9b'></fieldset></dl></div>
                <tfoot id='oFF9b'></tfoot>
                  <bdo id='oFF9b'></bdo><ul id='oFF9b'></ul>
                  <legend id='oFF9b'><style id='oFF9b'><dir id='oFF9b'><q id='oFF9b'></q></dir></style></legend>
                  本文介绍了子进程Popen和通信后关闭所有文件的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我们在运行 python Twisted 应用程序的 Ubuntu Linux 机器上遇到了一些可怕的打开的文件太多"的问题.在我们程序的许多地方,我们都在使用子进程 Popen,如下所示:

                  We are having some problems with the dreaded "too many open files" on our Ubuntu Linux machine rrunning a python Twisted application. In many places in our program, we are using subprocess Popen, something like this:

                  Popen('ifconfig ' + iface, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
                  output = process.stdout.read()
                  

                  而在其他地方我们使用子进程通信:

                  while in other places we use subprocess communicate:

                  process = subprocess.Popen(['/usr/bin/env', 'python', self._get_script_path(script_name)],
                                         stdin=subprocess.PIPE,
                                         stdout=subprocess.PIPE,
                                         close_fds=True)
                  out, err = process.communicate(data)
                  

                  在这两种情况下我究竟需要做什么才能关闭任何打开的文件描述符?Python 文档对此并不清楚.根据我收集的信息(可能是错误的),communicate() 和 wait() 确实会自行清理任何打开的 fd.但是波本呢?如果我不调用通信或等待,我是否需要在调用 Popen 后显式关闭标准输入、标准输出和标准错误?

                  What exactly do I need to do in both cases in order to close any open file descriptors? Python documentation is not clear on this. From what I gather (which could be wrong) both communicate() and wait() will indeed clean up any open fds on their own. But what about Popen? Do I need to close stdin, stdout, and stderr explicitly after calling Popen if I don't call communicate or wait?

                  推荐答案

                  根据到子进程模块的这个源(链接) 如果你调用 communicate 你不应该需要关闭 stdoutstderr管道.

                  According to this source for the subprocess module (link) if you call communicate you should not need to close the stdout and stderr pipes.

                  否则我会尝试:

                  process.stdout.close()
                  process.stderr.close()
                  

                  在你使用完 process 对象之后.

                  after you are done using the process object.

                  例如,当你直接调用 .read() 时:

                  For instance, when you call .read() directly:

                  output = process.stdout.read()
                  process.stdout.close()
                  

                  查看上面的模块源代码,了解 communicate() 是如何定义的,你会看到它在读取每个管道后会关闭它,所以这也是你应该做的.

                  Look in the above module source for how communicate() is defined and you'll see that it closes each pipe after it reads from it, so that is what you should also do.

                  这篇关于子进程Popen和通信后关闭所有文件的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:具有等号和空格的 Python 子进程参数 下一篇:Python字符串作为子进程的文件参数

                  相关文章

                  1. <small id='wxwVp'></small><noframes id='wxwVp'>

                    <legend id='wxwVp'><style id='wxwVp'><dir id='wxwVp'><q id='wxwVp'></q></dir></style></legend>

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