和通信后关闭所有文件的正确方法

和通信后关闭所有文件的正确方法

本文介绍了在子进程 Popen 和通信后关闭所有文件的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在运行 python Twisted 应用程序的 Ubuntu Linux 机器上遇到了可怕的打开的文件太多"问题.在我们程序的很多地方,我们都使用了子进程 Popen,就像这样:

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

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

process = subprocess.Popen(['/usr/bin/env', 'python', self._get_script_path(script_name)],标准输入=子进程.PIPE,标准输出=子进程.PIPE,close_fds=真)出,错误 = process.communicate(data)

在这两种情况下我到底需要做什么才能关闭任何打开的文件描述符?Python 文档对此并不清楚.从我收集到的信息(这可能是错误的),communication() 和wait() 确实会自行清理任何打开的文件.但是波本呢?如果我不调用communication或wait,我是否需要在调用Popen后显式关闭stdin、stdout和stderr?

解决方案

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

否则我会尝试:

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

在您使用完 process 对象之后.

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

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

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

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)

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?

解决方案

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

Otherwise I would try:

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

after you are done using the process object.

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

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

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 和通信后关闭所有文件的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 16:01