Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!ecngs!feeder2.ecngs.de!novso.com!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.028 X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'sys': 0.07; 'cc:addr :python-list': 0.11; 'python': 0.11; "'r',": 0.16; '-tkc': 0.16; 'flush': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'reliably': 0.16; 'wrote:': 0.18; 'input': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'second': 0.26; 'header:In-Reply-To:1': 0.27; "doesn't": 0.30; '+0100,': 0.31; 'os,': 0.31; 'file': 0.32; 'fri,': 0.33; "can't": 0.35; 'but': 0.35; 'method': 0.36; 'received:10': 0.37; 'skip:o 20': 0.38; 'handle': 0.38; 'either': 0.39; 'read': 0.60; 'first': 0.61; 'account': 0.65; 'nobody': 0.68; 'reading,': 0.84; 'received:108': 0.93; 'to:addr:nobody': 0.96 X-Sender-Id: wwwh|x-authuser|tim@thechases.com X-Sender-Id: wwwh|x-authuser|tim@thechases.com X-MC-Relay: Neutral X-MailChannels-SenderId: wwwh|x-authuser|tim@thechases.com X-MailChannels-Auth-Id: wwwh X-MC-Loop-Signature: 1413653353712:870180929 X-MC-Ingress-Time: 1413653353712 Date: Sat, 18 Oct 2014 12:32:07 -0500 From: Tim Chase To: Nobody Subject: Re: Flush stdin In-Reply-To: References: X-Mailer: Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-AuthUser: tim@thechases.com Cc: python-list@python.org 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: 33 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1413653368 news.xs4all.nl 2885 [2001:888:2000:d::a6]:55683 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:79273 On 2014-10-18 17:55, Nobody wrote: > On Fri, 17 Oct 2014 12:38:54 +0100, Empty Account wrote: >=20 > > I am using netcat to listen to a port and python to read stdin > > and print to the console. > >=20 > > nc -l 2003 | python print_metrics.py > >=20 > > sys.stdin.flush() doesn=E2=80=99t seem to flush stdin, >=20 > You can't "flush" an input stream. You can't flush it, but you can make it unbuffered. You can either force python to use unbuffered stdio: python -u myfile.py or you can get an unbuffered handle to the file import os, sys buffer_size =3D 1 new_stdin =3D os.fdopen(sys.stdin.fileno(), 'r', buffer_size) for c in new_stdin: do_something(c) though based on my reading, the first method works with both Py2 and Py3k while the second method doesn't reliably work in Py3k. -tkc