[Python-Dev] Internal counter to debug leaking file descriptors

Giampaolo Rodolà g.rodola at gmail.com
Fri Sep 3 12:09:50 CEST 2010

> Of course it would be nice to get access to FD stack so that a
> full filename can also be retrieved in this case.

On Linux, this can be easily achieved by using /proc.
You can take a look at how this is done in the current development
version of psutil:

>>> import psutil, os
>>> this_process = psutil.Process(os.getpid())
>>> f = open('file.ext', 'w')
>>> this_process.get_open_files()

Same for sockets, a bunch of lines later:

>>> import socket
>>> s = socket.create_connection(('google.com', 80))
>>> this_process.get_connections()
[connection(family=2, type=1, local_address=('', 38067),
remote_address=('', 80), status='ESTABLISHED')]

Hope this helps

--- Giampaolo

More information about the Python-Dev mailing list