I'm trying to track down the root cause of this error:
[Errno 24] Too many open files
To better understand how this "open file" leak is happening, I want to be able to check at various points in my program how many open files the program is responsible for.
I read this post, which includes some interesting tips from the resource
module, along with this snippet:
import resource
import fcntl
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
The length of fds
, I guess, should give you the number of open files your program currently holds.
Is there a more direct way of getting this count? What other strategies should I use to track down these leaks?
For the record, I did also enable ResourceWarning
, but that didn't yield any complaints about open files. It did complain about unclosed sockets, but I'm not sure if that can contribute to a "Too many open files" error.