当我没有文件标识符时,如何关闭python中的文件 [英] How do I close files in python when I haven't got the file identifiers

查看:188
本文介绍了当我没有文件标识符时,如何关闭python中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现自己无法在Python中打开新文件。当我用 ls -l / proc / PID / fd 进行检查时,我看到了大量为Python进程打开的文件。我正在使用的模块显然是打开大量的文件,而不是关闭它们。

我期待,我可以通过删除与打开文件,但没有发生。



我也希望看到垃圾收集中的文件对象,但是我没有看到类似于打开的文件:

  for obj in gc.get_objects():
如果hasattr(obj,'read'):
print(obj)

当我退出Python时,这些文件就消失了。

解决方案

问题很可能是这些文件描述符在不关联到Python对象的情况下正在泄漏。 Python无法查看与Python对象无关的实际文件描述符(操作系统资源)。如果它们与Python对象相关联,那么Python会在收集垃圾时关闭它们。或者,第三方库自己也跟踪文件描述符。



您可以使用 os.close 纯整数来关闭关联的文件描述符。如果你知道你想保持打开哪个文件描述符(通常是stdin / stdout / stderr,它们是0,1和2,也可能是其他几个),你可以关闭所有其他的从0到65535的整数, / proc /< pid> / fd

  import os 
$ b KEEP_FD = set([0,1,2])

在os.listdir中的fd(os.path.join(/ proc,str(os。 getpid()),fd)):
如果int(fd)不在KEEP_FD中:
try:
os.close(int(fd))
OSError:
pass

虽然这是一个非常邪恶的黑客攻击。更好的解决方案是修复第三方库。


I found myself unable to open new files in Python. When I examined with ls -l /proc/PID/fd I saw loads of files open for the Python process. The module I am using is apparently opening lots of files and not closing them.

I was expecting that I could close the files by deleting objects associated with the module that opened the files, but nothing happened.

I was also expecting to see the file objects somewhere in the garbage collection, but I saw nothing that resembled the open files with this:

for obj in gc.get_objects():
    if hasattr(obj, 'read'):
        print(obj)

The files disappear when I quit Python.

解决方案

The problem is likely that those file descriptors are leaking without being associated to Python objects. Python has no way of seeing actual file descriptors (OS resources) that are not associated with Python objects. If they were associated with Python objects, Python would close them when they are garbage collected. Alternatively, the third-party library does its own tracking of file descriptors.

You can use os.close on plain integers to close the associated file descriptor. If you know which file descriptors you want to keep open (usually, stdin/stdout/stderr, which are 0, 1 and 2, and maybe a few others), you can just close all other integers from 0 to 65535, or simply those in /proc/<pid>/fd:

import os

KEEP_FD = set([0, 1, 2])

for fd in os.listdir(os.path.join("/proc", str(os.getpid()), "fd")):
    if int(fd) not in KEEP_FD:
        try:
            os.close(int(fd))
        except OSError:
            pass

This is a pretty evil hack, though. The better solution would be to fix the third-party library.

这篇关于当我没有文件标识符时,如何关闭python中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆