I want to create a child process with subprocess.Popen
and read a message from it using custom file descriptor. I have two files:
parent.py
:
import osimport subprocessr_fd, w_fd = os.pipe()os.set_inheritable(w_fd, True)print(f'parent.py - Writing fd: {w_fd}. Is inheritable - {os.get_inheritable(w_fd)}')fo = os.fdopen(r_fd)with subprocess.Popen(['python', 'child.py'], close_fds=False): print(fo.read())fo.close()
child.py
:
import osinherited_fd = 4try: is_inheritable = os.get_inheritable(inherited_fd)except OSError as err: print(f'child.py - os.get_inheritable error: {err}')fo = os.fdopen(inherited_fd)fo.write('hello')fo.close()
As far as I understand the documentation about Inheritance of File Descriptors, all I need is make my writing fd inheritable and set Popen
's close_fds
to False
. Then I can use it in my child process.
Executing python parent.py
gives the following output:
parent.py - Writing fd: 4. Is inheritable - Truechild.py - os.get_inheritable error: [Errno 9] Bad file descriptorTraceback (most recent call last): File "D:\temp\child.py", line 10, in <module> fo = os.fdopen(inherited_fd) File "C:\Python310\lib\os.py", line 1029, in fdopen return io.open(fd, mode, buffering, encoding, *args, **kwargs)OSError: [WinError 6] The handle is invalid
I do not mind hardcoded fd now, also it is not necessary for me to use Popen
. The only important condition is that I need to use custom fd and I am interested in making it work on Windows. I use Python 3.11. Do I miss something?