Python Glossary Русский

Python 3.4+Windowsosfunction

os.set_handle_inheritable()

os — file descriptors

Sets the inheritable flag of a Windows handle for child processes; Windows only — use os.set_inheritable() for regular file descriptors.

Syntax

os.set_handle_inheritable(handle, inheritable, /)

It changes the inheritance flag of a Windows HANDLE, not a C descriptor number. Since Python 3.4 created handles are non-inheritable by default, and this function lets you explicitly hand one to a child. Remember that every flagged handle is inherited — for targeted passing subprocess offers STARTUPINFO.lpAttributeList with handle_list.

Examples

import os
print(hasattr(os, 'set_handle_inheritable') == (os.name == 'nt'))   # → True (функция есть только на Windows)
if os.name == 'nt':
    import msvcrt
    fd = os.open(os.devnull, os.O_RDONLY)
    os.set_handle_inheritable(msvcrt.get_osfhandle(fd), True)   # разрешить наследование хендла
    os.close(fd)
r, w = os.pipe()
os.set_inheritable(r, True)    # кроссплатформенный аналог для обычных fd
print(os.get_inheritable(r))   # → True
os.close(r); os.close(w)

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy