Path: csiph.com!usenet.pasdenom.info!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed1.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'output': 0.05; 'context': 0.07; 'explicit': 0.07; 'indicating': 0.07; 'modify': 0.07; 'python3': 0.07; 'skip:u 30': 0.07; 'sys': 0.07; '3),': 0.09; 'bash': 0.09; 'dan': 0.09; 'protocols,': 0.09; 'thus,': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'skip:= 70': 0.12; '(there': 0.16; '-1,': 0.16; '4096)': 0.16; 'bit.': 0.16; 'buffering,': 0.16; 'cc:name:python list': 0.16; 'character.': 0.16; 'run:': 0.16; 'running:': 0.16; 'sink': 0.16; 'tcp': 0.16; 'true:': 0.16; 'underlying': 0.16; 'elements': 0.16; 'wrote:': 0.18; 'pieces': 0.19; 'import': 0.22; 'preferred': 0.22; 'saying': 0.22; 'cc:addr:python.org': 0.22; "aren't": 0.24; 'issue,': 0.24; 'skip:{ 20': 0.24; 'mon,': 0.24; 'versions': 0.24; '(or': 0.24; 'cc:2**0': 0.24; 'nearly': 0.26; 'switch': 0.26; 'least': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'character': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; 'getting': 0.31; 'lines': 0.31; '120': 0.31; 'away.': 0.31; "we're": 0.32; 'becomes': 0.33; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'data,': 0.36; 'done': 0.36; 'useful': 0.36; "i'll": 0.36; 'url:org': 0.36; 'two': 0.37; 'performance': 0.37; 'depends': 0.38; 'issue': 0.38; 'pm,': 0.38; 'that,': 0.38; 'does': 0.39; 'skip:p 20': 0.39; 'even': 0.60; 'then,': 0.60; 'break': 0.61; 'you.': 0.62; 'back': 0.62; 'times': 0.62; "you've": 0.63; 'more': 0.64; 'different': 0.65; 'worth': 0.66; 'believe': 0.68; '20,': 0.68; 'fact,': 0.69; 'acts': 0.74; '"not': 0.84; 'demonstrates': 0.84; 'messed': 0.84; 'picture.': 0.84; 'prey': 0.84; 'habit': 0.91; 'story.': 0.93; 'suffer': 0.93 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; bh=CsAXWXlWSYvsIdV7Mz+BpsepPuBIb0CZ3cwqNat2AQU=; b=IOHYwRVIuoh8HrEVqDWJa9GwRdxKPrHKu/pntHB+eELM230VGvZ6bCmh24DS5UAX70 7x3TWZsNx0562oO4Mck7it7Qh7hByTZ40b/BtnMByzMzBEkbUR+5yEKanclG/QIE/r6D wXT7gH9piZgA7YGL3RaiibmQq6CbrfEXrS2hVfBLwj8Ge/40dhYmK6fgOlxO/Gx/uBjr sq4g3owcS74elxNG9fUK95lg+6x0cqXxqh22oiLhtV1fgJrXew6KYBVCVbAwnCAECDqr e4ESHL2cFS2hNYeGewvxVhNA7bDEZ0ntG+/CUY576TOnWjF6kebxDbNEpLd8N0uvaEZ8 0PtQ== MIME-Version: 1.0 X-Received: by 10.152.5.169 with SMTP id t9mr6496592lat.90.1413851413670; Mon, 20 Oct 2014 17:30:13 -0700 (PDT) In-Reply-To: <87mw8qfhza.fsf@elektro.pacujo.net> References: <87a94rgxi5.fsf@elektro.pacujo.net> <87mw8qfhza.fsf@elektro.pacujo.net> Date: Mon, 20 Oct 2014 17:30:13 -0700 Subject: Re: Flush stdin From: Dan Stromberg To: Marko Rauhamaa Content-Type: text/plain; charset=UTF-8 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: 103 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1413851421 news.xs4all.nl 2877 [2001:888:2000:d::a6]:49597 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:79359 On Mon, Oct 20, 2014 at 4:18 PM, Marko Rauhamaa wrote: > Dan Stromberg : >> ...then everything acts line buffered, or perhaps even character >> buffered [...] >> >> That, or we're using two different versions of netcat (there are at >> least two available). > > Let's unconfuse the issue a bit. I'll take line buffering, netcat and > the OS out of the picture. > > Here's a character generator (test.sh): > ======================================================================== > while : ; do > echo -n x > sleep 1 > done > ======================================================================== > > and here's a character sink (test.py): > ======================================================================== > import sys > while True: > c = sys.stdin.read(1) > if not c: > break > print(ord(c[0])) > ======================================================================== > > Then, I run: > ======================================================================== > $ bash ./test.sh | python3 ./test.py > 120 > 120 > 120 > 120 > ======================================================================== > > The lines are output at one-second intervals. > > That demonstrates that sys.stdin.read(1) does not block for more than > one character. IOW, there is no buffering whatsoever. Aren't character-buffered and unbuffered synonymous? Often with TCP protocols, line buffered is preferred to character buffered, both for performance and for simplicity: it doesn't suffer from tinygrams (as much), and telnet becomes a useful test client. Also, it's a straightforward way of framing your data, to avoid getting messed up by Nagle or fragmentation. One might find http://stromberg.dnsalias.org/~strombrg/bufsock.html worth a glance. It's buffered, but it keeps things framed, and doesn't fall prey to tinygrams nearly as much as character buffering. > If I change the sink a bit: "c = sys.stdin.read(5)", I get the same > output but at five-second intervals indicating that sys.stdin.read() > calls the underlying os.read() function five times before returning. In > fact, that conclusion is made explicit by running: > > ======================================================================== > $ bash ./test.sh | strace python3 ./test.py > ... > read(0, "x", 4096) = 1 > read(0, "x", 4096) = 1 > read(0, "x", 4096) = 1 > read(0, "x", 4096) = 1 > read(0, "x", 4096) = 1 > fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 3), ...}) = 0 > mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f3143bab000 > write(1, "120\n", 4120 > ) = 4 > ... ======================================================================== This is tremendously inefficient. It demands a context switch for every character. > If I modify test.py to call os.read(): > ======================================================================== > import os > while True: > c = os.read(0, 5) > if not c: > break > print(ord(c[0])) > ======================================================================== > > The output is again printed at one-second intervals: no buffering. > > Thus, we are back at my suggestion: use os.read() if you don't want > Python to buffer stdin for you. It's true that Python won't buffer (or will be character-buffered) then, but that takes some potentially-salient elements out of the picture. IOW, I don't think Python reading unbuffered is necessarily the whole issue, and may even be going to far. I have a habit of saying "necessary, but not necessarily sufficient", but in this case I believe it's more of a "not necessarily necessary, and not necessarily sufficient". A lot depends on the other pieces of the puzzle that you've chosen to "unconfuse" away. Yes, you can make Python unbuffered/character-buffered, but that's not the whole story.