Too Many Files Open

After about 15 minutes of execution, my Python program gets an error message that Too Many Files Are Open. The program executes a loop of code every second.

Every file open statement is done using a with open and every spi.open has a corresponding spi.close.

I imported psutil and added the code below in the program loop:

for f in psutil.Process().open_files()
    print(f)

I get no output from the print(f) statement.
I would appreciate any help in resolving the Too Many Files Open issue.

Unless you’re happy with uninformed wild guesswork, nobody can help you very much without seeing the full code.

1 Like

To see the output of print try this

print(f, flush=True)

Assuming you are on Linux system…

My guess is that one of the processes that psutils.Process() returns has a very large number of files open and your process ulimit does not allow you open that many file descriptors.

By default the limit is 1024 on my Fedora systems.

$ ulimit -a
...
open files                          (-n) 1024
...

Try raising the limit with the ulimit command.