Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: eryk sun Newsgroups: comp.lang.python Subject: Re: Passing File Descriptors To Subprocesses Date: Sun, 17 Jul 2016 03:00:35 +0000 Lines: 51 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de ZuImcuJ8iJUEh6FNd9vMBgDSpkpiePqIyTorbbB5EGdA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.016 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'seemed': 0.07; 'example:': 0.10; 'python': 0.10; '2016': 0.16; 'descriptors': 0.16; 'exit()': 0.16; 'fcntl': 0.16; 'parent:': 0.16; 'pipes': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:File': 0.16; 'subprocess': 0.16; 'wrote:': 0.16; 'settings.': 0.18; '>>>': 0.20; 'arguments': 0.22; 'lawrence': 0.22; 'os,': 0.22; 'pass': 0.22; 'am,': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'url:bugs': 0.24; 'linux': 0.26; 'message- id:@mail.gmail.com': 0.27; 'specify': 0.27; 'pep': 0.29; 'url:peps': 0.29; 'url:python': 0.33; 'flags': 0.33; 'open': 0.33; 'correctly': 0.34; 'file': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'false': 0.35; 'url:dev': 0.35; 'url:org': 0.36; 'child': 0.36; 'url:library': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'skip:o 20': 0.38; 'to:addr:python.org': 0.40; 'called': 0.40; 'url:3': 0.60; 'matter': 0.63; 'more': 0.63; 'to,': 0.63; 'mar': 0.65; 'skip:\xe2 10': 0.70; '8bit%:40': 0.72; 'jul': 0.72; '5.3.1': 0.84; 'child,': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-transfer-encoding; bh=/gsFnMexbOYP3z4CSDgkz2hTeT6PiyTZcmDjrb5X64E=; b=sAZRh9QHPq5RSQ9f7MwjIenBxiACZ5Iha2SlFaZbNu6xS4jIgEIxWSu78lq1ar/pzd 9ElX0F0QcD+5fyvJ7EbubBMpQJOjKn5a3/Y014+Vc8CqCsBHicExBSTmvMMrIGOWrJgf e0ptOEc2lHPMgFyFQ4b61qxSCShY/8X5I9jHuu7Wew69/MXK5MIsk2zqu7X2f1zG4v6H VZ3qdVnkwEw0p4KT7hKWKVJUpjopMhppMz2hG2XFCWfLM1wOfwjOKoEXGxRuxqL6R95r CIaU98SDBuSNmSU1hdpUPP+jIdh3OlTBiVDnJzsAltHyD3SOknQCdF6+wHVhOcgdyGXx uJRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:content-transfer-encoding; bh=/gsFnMexbOYP3z4CSDgkz2hTeT6PiyTZcmDjrb5X64E=; b=YibS/C6q5hzXVLYI7AsEGRqiyB9ow6XcGhl4G+X51DdDXRweCTFbTc7VqMLpbw3Dzd vUjVuqO02btz3gOCek8mzapFnC4OfIJMODEgpnp1UbjJ25zxCgj2ucEZ8uP16yeLAXxT kYuBapKyrq6mTVZGFLdCtvP8KiK6ga+EGCPtCz0df/+Yz0tL8oDKxKQCz5uEX5X8NnNK oVIfrSDKbvmDfsrubU23vXA6ufmvswqBlSR5pvRKsF1XTyw9uOIHfjVrTUPovpEUZf8s xRmpDJTeCyUX1FyZktMcsLaTBcLICHltl+jl1pHc2lI7z2TYak8tvTpohjOzlfe45xxt C6gg== X-Gm-Message-State: ALyK8tLn8Mw1EyPMUBrjDuFVwfqmFo0DRaLQTufYYrwTzi0DrKU02t8pzgXEyX7IifXRtw8hmd5E3jOZ8Wecdw== X-Received: by 10.36.37.146 with SMTP id g140mr27969101itg.58.1468724475276; Sat, 16 Jul 2016 20:01:15 -0700 (PDT) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: Xref: csiph.com comp.lang.python:111541 On Sun, Jul 17, 2016 at 1:59 AM, Lawrence D=E2=80=99Oliveiro wrote: > The various subprocess functions have > arguments called =E2=80=9Cclose_fds=E2=80=9D and =E2=80=9Cpass_fds=E2=80= =9D, which specify which file descriptors are to be > left open in the child process. Yet no matter what I set these to, it see= med I could not pass > my pipes to a subprocess. > > What the docs *don=E2=80=99t* tell you is that these arguments do not con= trol 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 =3D os.pipe() >>> fdw 4 >>> os.get_inheritable(fdw) False >>> subprocess.call(['python3'], pass_fds=3D[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'