Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #111541

Re: Passing File Descriptors To Subprocesses

From eryk sun <eryksun@gmail.com>
Newsgroups comp.lang.python
Subject Re: Passing File Descriptors To Subprocesses
Date 2016-07-17 03:00 +0000
Message-ID <mailman.52.1468724484.2307.python-list@python.org> (permalink)
References <f421033d-5fa4-4d02-8c3e-dadc8b778e44@googlegroups.com> <CACL+1atgkjXMvy0+45pSXpVf0xxT7=iDpZDAMiHN0tP6XTd5rA@mail.gmail.com>

Show all headers | View raw


On Sun, Jul 17, 2016 at 1:59 AM, Lawrence D’Oliveiro
<lawrencedo99@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'

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Passing File Descriptors To Subprocesses Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-07-16 18:59 -0700
  Re: Passing File Descriptors To Subprocesses eryk sun <eryksun@gmail.com> - 2016-07-17 03:00 +0000
    Re: Passing File Descriptors To Subprocesses Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-07-17 00:06 -0700

csiph-web