Too many open files in python

后端 未结 3 1275
离开以前
离开以前 2020-12-05 23:52

I wrote kind of a test suite which is heavily file intensive. After some time (2h) I get an IOError: [Errno 24] Too many open files: \'/tmp/tmpxsqYPm\'. I doubl

3条回答
  •  遥遥无期
    2020-12-06 00:25

    The corrected code is:

    import resource
    import fcntl
    import os
    
    def get_open_fds():
        fds = []
        soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE)
        for fd in range(0, soft):
            try:
                flags = fcntl.fcntl(fd, fcntl.F_GETFD)
            except IOError:
                continue
            fds.append(fd)
        return fds
    
    def get_file_names_from_file_number(fds):
        names = []
        for fd in fds:
            names.append(os.readlink('/proc/self/fd/%d' % fd))
        return names
    
    fds = get_open_fds()
    print get_file_names_from_file_number(fds)
    

提交回复
热议问题