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


Groups > comp.lang.python > #79403

Re: Flush stdin

Path csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'read.': 0.03; 'cpython': 0.05; 'context': 0.07; 'python3': 0.07; '3),': 0.09; 'bash': 0.09; 'bytes,': 0.09; 'bytes.': 0.09; 'dan': 0.09; 'indeed,': 0.09; 'protocols,': 0.09; 'second.': 0.09; 'tests,': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'skip:= 70': 0.12; 'wrote': 0.14; 'question.': 0.14; '(when': 0.16; '-1,': 0.16; '...,': 0.16; '1),': 0.16; '4096)': 0.16; 'blocks': 0.16; 'cc:name:python list': 0.16; 'character.': 0.16; 'higher-level': 0.16; 'illusion': 0.16; 'irrespective': 0.16; 'libraries.': 0.16; 'merged': 0.16; 'op?': 0.16; 'stuff,': 0.16; 'tcp': 0.16; 'tty': 0.16; 'url:linux': 0.16; 'write.': 0.16; 'wrote:': 0.18; 'library': 0.18; 'bit': 0.19; '(but': 0.19; 'normally': 0.19; '>>>': 0.22; 'input': 0.22; 'preferred': 0.22; 'cc:addr:python.org': 0.22; 'affects': 0.24; "aren't": 0.24; 'byte': 0.24; 'bytes': 0.24; 'driver': 0.24; 'skip:{ 20': 0.24; 'mon,': 0.24; "haven't": 0.24; '(or': 0.24; 'cc:2**0': 0.24; 'developers': 0.25; 'switch': 0.26; 'code:': 0.26; 'header:In- Reply-To:1': 0.27; 'function': 0.29; 'character': 0.29; 'related': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'getting': 0.31; 'requests': 0.31; 'helpful.': 0.31; 'with,': 0.31; 'probably': 0.32; 'stuff': 0.32; 'guess': 0.33; 'role': 0.34; 'could': 0.34; 'problem': 0.35; 'created': 0.35; 'something': 0.35; "who's": 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'really': 0.36; 'data,': 0.36; 'doing': 0.36; 'shows': 0.36; 'useful': 0.36; 'url:org': 0.36; 'should': 0.36; 'example,': 0.37; 'application': 0.37; 'two': 0.37; 'sometimes': 0.38; 'issue': 0.38; 'pm,': 0.38; 'little': 0.38; 'anything': 0.39; 'supporting': 0.39; 'skip:p 20': 0.39; 'space': 0.40; 'even': 0.60; 'read': 0.60; 'devices': 0.61; 'url:3': 0.61; 'took': 0.61; 'complete': 0.62; 'show': 0.63; 'our': 0.64; 'interest': 0.64; 'more': 0.64; 'series': 0.66; 'between': 0.67; '20,': 0.68; 'reads': 0.68; 'useful.': 0.68; 'helping': 0.70; 'actually,': 0.84; 'examples.': 0.84; 'messed': 0.84; 'relate': 0.84; 'capture': 0.91; 'essence': 0.91; 'from.': 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=upWDdXXLJzNihP0za/wRpge0pf7tVZ0wueAHDNQYr/4=; b=h6x6DkWlFnAxSh+NlU6HdhqVQStWVC7G/4561alegwD5Cdd0vSVjCriHBnShonbrHq I68erqv1gADcp2DyHBmgc/DrZdzp2d/E6yuc9yS6JnodPX5eyun9+f4btCk5IfnsTTRr T4IDkS7v18Xc5SfD8vctbefLRMQx6gDlgUIjS+nB3cKL4d1RoQ6l/b6s+I/9/seK279o n4PzKZCE3N1rSzDmffYZthYzJ6N8epO4uOOhyonXg4FD6zF8km0pmWuYs1WzP3knxYQc yydtZRKsyZGtwE2wX8kLV3lLbkk/p/QtAce7XG+9s/16vEohF4kGkzBWKM9KyJarvUAq Lvgg==
MIME-Version 1.0
X-Received by 10.152.198.204 with SMTP id je12mr38345588lac.61.1413933393210; Tue, 21 Oct 2014 16:16:33 -0700 (PDT)
In-Reply-To <87h9yyf318.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> <mailman.15019.1413851421.18130.python-list@python.org> <87h9yyf318.fsf@elektro.pacujo.net>
Date Tue, 21 Oct 2014 16:16:33 -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.15049.1413933396.18130.python-list@python.org> (permalink)
Lines 90
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1413933396 news.xs4all.nl 2913 [2001:888:2000:d::a6]:33507
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:79403

Show key headers only | View raw


On Mon, Oct 20, 2014 at 9:41 PM, Marko Rauhamaa <marko@pacujo.net> wrote:
> Dan Stromberg <drsalists@gmail.com>:
>
>> Often with TCP protocols, line buffered is preferred to character
>> buffered,
>
> Terminal devices support line buffering on write.

Yes, though that's not the only place it's useful.

> Line buffering on read is an illusion created by higher-level libraries.
> The low-level read function reads in blocks of bytes.

Actually, doesn't line buffering sometimes exist inside an OS kernel?
stty/termios/termio/sgtty relate here, for *ix examples.  Supporting
code: http://stromberg.dnsalias.org/~strombrg/ttype/  It turns on
character-at-a-time I/O in the tty driver via a variety of methods for
portability.  I wrote it in C before I took an interest in Python.

Also, here's some supporting documentation:
http://man7.org/linux/man-pages/man3/stdout.3.html - excerpt:
    Indeed, normally terminal input is line buffered in the kernel.

But even if line buffering (or even character buffering) were never in
the kernel, calling it an illusion is perhaps going a little far.
It's useful sometimes, irrespective of where it comes from.
"Illusion" has a bit of an undeserved pejorative connotation.

>> Also, it's a straightforward way of framing your data, to avoid
>> getting messed up by Nagle or fragmentation.
>
> Nagle affects the communication between the peer OS kernels and isn't
> directly related to anything the application does.

Actually, Nagle can cause two or more small packets to be merged,
which is something an application must be able to deal with, because
they could show up in the receiving application as one or more (but
anyway: fewer) merged recv()'s.  That's one reason why something like
http://stromberg.dnsalias.org/~strombrg/bufsock.html can be helpful.

> Also, Nagle doesn't
> play any role with pipes.

Yes, but pipes aren't the only thing involved in the OP's question.
You "simplified" the problem down to pipes, but that doesn't really
capture the complete essence of the matter.  Nagle is one of the
reasons.

>>> ========================================================================
>>> $ 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.
>
> Inefficiency isn't an issue when you generate one byte a second.

Of course, but who's doing one byte per second?  You and I in our
tests, and perhaps some application developers with remarkably
undemanding I/O.  That doesn't really mean we should _recommend_ a
series of os.read(0, 1)'s.

> If data
> were generated at a brisker pace, "read(0, ..., 4096)" could get more
> bytes at a time. Notice that even if the Python code requests 5 bytes,
> CPython requests up to 4096 bytes in a single read.

Not if you use os.read(0, 1), for example, which was what you appeared
to be recommending.  os.read(0, 1) (when on a pipe) makes a call into
kernel space via a context switch, once for each os.read(0, 1).

I guess I should add that when you do an os.read(0, 1), and see it
show up in strace, strace is showing kernel<->userspace interactions,
not library stuff, and not stuff in an application that sits above
libraries.  ltrace shows some of the library stuff, but probably not
all of it - I haven't studied ltrace as much as I have strace.

Just wondering: Are we helping the OP?

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


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