Passing File Descriptors To Subprocesses
eryk sun
eryksun at gmail.com
Sat Jul 16 23:00:35 EDT 2016
On Sun, Jul 17, 2016 at 1:59 AM, Lawrence D’Oliveiro
<lawrencedo99 at gmail.com> wrote:
> The various subprocess functions <https://docs.python.org/3/library/subprocess.html> have
> arguments called “close_fds” and “pass_fds”, which specify which file descriptors are to be
> left open in the child process. Yet no matter what I set these to, it seemed I could not pass
> my pipes to a subprocess.
>
> What the docs *don’t* tell you is that these arguments do not control what happens after the
> exec. The file descriptors that are kept open are only those which do not have the
> FD_CLOEXEC flags set in their fcntl settings.
It works correctly in 3.4+, which makes the pass_fds file descriptors
inheritable in the child, after fork. See issue 18571 and PEP 446,
section "Other Changes":
http://bugs.python.org/issue18571
https://www.python.org/dev/peps/pep-0446/#other-changes
For example:
Python 3.5.1+ (default, Mar 30 2016, 22:46:26)
[GCC 5.3.1 20160330] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os, subprocess
>>> fdr, fdw = os.pipe()
>>> fdw
4
>>> os.get_inheritable(fdw)
False
>>> subprocess.call(['python3'], pass_fds=[fdw])
child:
Python 3.5.1+ (default, Mar 30 2016, 22:46:26)
[GCC 5.3.1 20160330] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.write(4, b'spam')
4
>>> exit()
parent:
0
>>> os.read(fdr, 4)
b'spam'
More information about the Python-list
mailing list