Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.014 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'causing': 0.04; 'output': 0.05; 'excluding': 0.09; 'output,': 0.09; 'rewrite': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'cc:name:python list': 0.16; 'exited.': 0.16; 'flush': 0.16; 'tty': 0.16; 'url:ftp': 0.16; 'url:html)': 0.16; 'url:sources': 0.16; 'url:unix': 0.16; 'which,': 0.16; 'sat,': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'input': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'cc:2**0': 0.24; 'script': 0.25; 'header:In-Reply-To:1': 0.27; 'generally': 0.29; 'tim': 0.29; 'timely': 0.30; 'message- id:@mail.gmail.com': 0.30; 'along': 0.30; "i'm": 0.30; '+0100,': 0.31; 'agreed.': 0.31; 'chase': 0.31; 'pipe': 0.31; 'url:se': 0.31; 'file': 0.32; 'probably': 0.32; '(i.e.': 0.33; 'alone': 0.33; 'fri,': 0.33; "i'd": 0.34; 'could': 0.34; "can't": 0.35; 'but': 0.35; 'received:google.com': 0.35; 'url:org': 0.36; 'should': 0.36; 'effort': 0.37; 'being': 0.38; 'filled': 0.38; 'requiring': 0.38; 'needed': 0.38; 'pm,': 0.38; 'sure': 0.39; 'enough': 0.39; 'either': 0.39; 'even': 0.60; 'read': 0.60; 'helps': 0.61; "you're": 0.61; 'kind': 0.63; 'account': 0.65; 'nobody': 0.68; 'manner.': 0.74; '(let': 0.84; '6:11': 0.84; 'room,': 0.93; 'thereafter': 0.93; 'to:addr:nobody': 0.96 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=qQzGLRb5t09X+W8F9zzM2Lm++CcjDnR3LZVgpKnPx6c=; b=eLIWtpWChe2ktPGpNf2d1TijK2IjxjkD7SJXnMiSuFpeWHzjKc3/yge7s/12A8goQr hBxIKfNoR64sQl7F3AviBuKGiOciDJ90w36LRyAm+pyOySUYtfLoozDSez2cyPrro3CD BCCnJnO00wIWbB+/AKbh7gshRIjZMS9yWSl43Cu38OTBosuODWJ/3W7IP0E+GDv5EVfV tbE+Bn3JL5G5tP6QKkbkiA/owBwSBPN5t2ZfYlwFHyN3gXiztvg+8NbsF34hKlifPajc sgVsZdnKw7OhuiPMWdthHsdYN7SqQqSWcHm6aNCaPYFph+1VaV2uxlv/BzAfIW2WskvA jyeA== MIME-Version: 1.0 X-Received: by 10.152.27.134 with SMTP id t6mr17947145lag.17.1413682491955; Sat, 18 Oct 2014 18:34:51 -0700 (PDT) In-Reply-To: References: Date: Sat, 18 Oct 2014 18:34:51 -0700 Subject: Re: Flush stdin From: Dan Stromberg To: Nobody Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: Python List X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 50 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1413682500 news.xs4all.nl 2916 [2001:888:2000:d::a6]:51352 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:79283 On Sat, Oct 18, 2014 at 6:11 PM, Nobody wrote: > On Sat, 18 Oct 2014 12:32:07 -0500, Tim Chase wrote: > >> On 2014-10-18 17:55, Nobody wrote: >>> On Fri, 17 Oct 2014 12:38:54 +0100, Empty Account wrote: >>> >>> > I am using netcat to listen to a port and python to read stdin and >>> > print to the console. >>> > >>> > nc -l 2003 | python print_metrics.py >>> > >>> > sys.stdin.flush() doesn=E2=80=99t seem to flush stdin, >>> >>> You can't "flush" an input stream. >> >> You can't flush it, but you can make it unbuffered. You can either forc= e >> python to use unbuffered stdio: > > [snipped] > > None of this helps in any way, as it's not the behaviour of the Python > script which is causing the problem, but that "nc" is (probably) bufferin= g > its output, so the data isn't passed to the OS (let alone to the Python > script) in a timely manner. Agreed. > Once the "nc" process actually write()s the data to its standard > output (i.e. desriptor 1, not the "stdout" FILE*) I'm not sure why you're excluding stdout, but even if nc is using filedes 1 instead of FILE * stdout, isn't it kind of irrelevant? > it will be available to > the Python script immediately thereafter without requiring any low-level > tweaks. Which, on a pipe, generally means either the buffer filled and needed to be passed along to make room, or the process exited. I'd probably rewrite just enough nc in Python to make it so you don't need to depend on a pipe (example: http://stromberg.dnsalias.org/~strombrg/pnetcat.html), but if you're on *ix you could try http://ftp.sunet.se/pub/usenet/ftp.uu.net/comp.sources.unix/volume23/pty/ in an effort to persuade nc to think that it's on a tty and hence should output line buffered data instead of block buffered - despite being on a pipe in reality. HTH.