Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #79359
| 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 | <drsalists@gmail.com> |
| 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 | <CAGGBd_r7tdPc1ZTA380-9aNRnbr6Dm3HzhG2cCH2sJwWn=c5iQ@mail.gmail.com> <mailman.14990.1413777392.18130.python-list@python.org> <87a94rgxi5.fsf@elektro.pacujo.net> <CAGGBd_qsZ=-8sn0YQPT1XQf9Qcn9cJ4Aw=hgR6=at0KzjN3xsg@mail.gmail.com> <mailman.15012.1413845578.18130.python-list@python.org> <87mw8qfhza.fsf@elektro.pacujo.net> |
| Date | Mon, 20 Oct 2014 17:30:13 -0700 |
| Subject | Re: Flush stdin |
| From | Dan Stromberg <drsalists@gmail.com> |
| To | Marko Rauhamaa <marko@pacujo.net> |
| Content-Type | text/plain; charset=UTF-8 |
| Cc | Python List <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.15019.1413851421.18130.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
On Mon, Oct 20, 2014 at 4:18 PM, Marko Rauhamaa <marko@pacujo.net> wrote:
> Dan Stromberg <drsalists@gmail.com>:
>> ...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.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Re: Flush stdin Cameron Simpson <cs@zip.com.au> - 2014-10-20 14:56 +1100
Re: Flush stdin Marko Rauhamaa <marko@pacujo.net> - 2014-10-20 07:45 +0300
Re: Flush stdin Dan Stromberg <drsalists@gmail.com> - 2014-10-20 14:59 -0700
Re: Flush stdin Marko Rauhamaa <marko@pacujo.net> - 2014-10-21 01:54 +0300
Re: Flush stdin Dan Stromberg <drsalists@gmail.com> - 2014-10-20 15:52 -0700
Re: Flush stdin Marko Rauhamaa <marko@pacujo.net> - 2014-10-21 02:18 +0300
Re: Flush stdin Dan Stromberg <drsalists@gmail.com> - 2014-10-20 17:30 -0700
Re: Flush stdin Marko Rauhamaa <marko@pacujo.net> - 2014-10-21 07:41 +0300
Re: Flush stdin Dan Stromberg <drsalists@gmail.com> - 2014-10-21 16:16 -0700
Re: Flush stdin Marko Rauhamaa <marko@pacujo.net> - 2014-10-22 08:38 +0300
Re: Flush stdin Chris Angelico <rosuav@gmail.com> - 2014-10-22 17:23 +1100
Re: Flush stdin Marko Rauhamaa <marko@pacujo.net> - 2014-10-22 13:57 +0300
Re: Flush stdin random832@fastmail.us - 2014-10-22 11:01 -0400
Re: Flush stdin Marko Rauhamaa <marko@pacujo.net> - 2014-10-22 18:28 +0300
csiph-web