Preventing file handle inheritance in multiprocessing lib Preventing file handle inheritance in multiprocessing lib windows windows

Preventing file handle inheritance in multiprocessing lib


The fileno() method returns the file number as assigned by the runtime library. Given the file number, you can then call msvcrt.get_osfhandle() to get the Win32 file handle. Use this handle in the call to SetHandleInformation. So something like the following may work:

win32api.SetHandleInformation(    msvcrt.get_osfhandle(testFile.fileno()),    win32api.HANDLE_FLAG_INHERIT,    0)

I'm not certain of the exact usage of the win32api module, but this should help bridge the gap between a Python file object and a Win32 handle.


I don't know about the multiprocessing module, but with the subprocess module you can instruct it to not inherit any file descriptors:

If close_fds is true, all file descriptors except 0, 1 and 2 will be closed before the child process is executed. (Unix only). Or, on Windows, if close_fds is true then no handles will be inherited by the child process. Note that on Windows, you cannot set close_fds to true and also redirect the standard handles by setting stdin, stdout or stderr.

Alternatively you could close all file descriptors in your child process with os.closerange

Close all file descriptors from fd_low (inclusive) to fd_high (exclusive), ignoring errors. Availability: Unix, Windows.


After you open a file handle, you can use the SetHandleInformation() function to remove the HANDLE_FLAG_INHERIT flag.