fork/exec & close file descriptors

Skip Montanaro skip.montanaro at gmail.com
Tue Jun 2 09:58:54 EDT 2015


Reviving (and concluding) a thread I started a couple weeks ago, I asked:

> The basic fork/exec dance is not a problem, but how do I discover
> all the open file descriptors in the new child process to make sure
> they get closed? Do I simply start at fd 3 and call os.close() on
> everything up to some largish fd number?

I wanted this again today (for different reasons than before).
Googling for "python close all file descriptors" returned the os
module docs as the first hit, and lo and behold, what do I see
documented? os.closerange (new in 2.6):

    os.closerange(fd_low, fd_high)
    Close all file descriptors from fd_low (inclusive) to fd_high
    (exclusive), ignoring errors.

Guido's time machine strikes again...

Skip



More information about the Python-list mailing list