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


Groups > comp.lang.python > #79273

Re: Flush stdin

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 <python.list@tim.thechases.com>
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 <python.list@tim.thechases.com>
To Nobody <nobody@nowhere.invalid>
Subject Re: Flush stdin
In-Reply-To <pan.2014.10.18.16.55.19.164000@nowhere.invalid>
References <mailman.14954.1413612650.18130.python-list@python.org> <pan.2014.10.18.16.55.19.164000@nowhere.invalid>
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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.14970.1413653368.18130.python-list@python.org> (permalink)
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

Show key headers only | View raw


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’t 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
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 = 1
 new_stdin = 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



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


Thread

Flush stdin Empty Account <emptya45@gmail.com> - 2014-10-17 12:38 +0100
  Re: Flush stdin Nobody <nobody@nowhere.invalid> - 2014-10-18 17:55 +0100
    Re: Flush stdin Tim Chase <python.list@tim.thechases.com> - 2014-10-18 12:32 -0500
      Re: Flush stdin Nobody <nobody@nowhere.invalid> - 2014-10-19 02:11 +0100
        Re: Flush stdin Dan Stromberg <drsalists@gmail.com> - 2014-10-18 18:34 -0700
        Re: Flush stdin Dan Stromberg <drsalists@gmail.com> - 2014-10-18 18:42 -0700
          Re: Flush stdin Nobody <nobody@nowhere.invalid> - 2014-10-22 03:49 +0100
            Re: Flush stdin Dan Stromberg <drsalists@gmail.com> - 2014-10-21 20:11 -0700
    Re: Flush stdin MRAB <python@mrabarnett.plus.com> - 2014-10-18 18:55 +0100
    Re: Flush stdin Cameron Simpson <cs@zip.com.au> - 2014-10-19 08:01 +1100
    Re: Flush stdin Terry Reedy <tjreedy@udel.edu> - 2014-10-18 18:35 -0400

csiph-web