Skip to main content

Work with file descriptors in a more human way

Project description


fdinfo is a python library for using file descriptors in a more intuitive way.


Creating a socket with SO_REUSEADR (or SO_REUSEPORT on BSD):

from filedes import FD
s = socket.socket()

Enabling non-blocking mode on a PIPE

import os
from filedes import FD
r, w = os.pipe()

Disabling the close on execute bit for a temporary file

import tempfile
from filedes import FD
tf = tempfile.NamedTemporaryFile()

Listing the open FDs and their types of the current PID:

from filedes import FD, get_open_fds
for fd in get_open_fds():
    print fd, FD(fd).typestr

Sending fds to another process over a unix socket

from filedes import FD

# Send a single FD

# Or for multiple FDs

# To receive one fd (in a different pid)
an_fd = FD(sock).socket.recv_fd()

# To receive multiple fds (in a different pid)
two_fds = FD(sock).socket.recv_fds(2)

Key features

  • Get detailed info about fds in both the local as well as external processes

  • Perform fcntl and io operations directly on descriptor objects

  • Unittest helper class for making sure your tests don’t leak fds or threads.

  • Set various fd flags in a more native way, such as fd.set_cloexec() or fd.set_nonblocking()

  • Socket helpers let you set socket options in a more native way, such as fd.socket.set_reuse()

  • A Popen() subclass with a more intelligent close_fds for systems with a high fs.file-max set


fdinfo is primarily developed on linux, but it doesn’t work just there. It has been tested on the following platforms:

  • linux

  • OSX

Developing for Darwin is tricky since there is no procfs, so some operations on filedescriptors in different pids may not work as desired

Project details

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page