Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!us.feeder.erje.net!news2.arglkargh.de!news.swapon.de!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Marko Rauhamaa Newsgroups: comp.lang.python Subject: Re: Flush stdin Date: Tue, 21 Oct 2014 07:41:07 +0300 Organization: A noiseless patient Spider Lines: 43 Message-ID: <87h9yyf318.fsf@elektro.pacujo.net> References: <87a94rgxi5.fsf@elektro.pacujo.net> <87mw8qfhza.fsf@elektro.pacujo.net> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="ff5cf27ef3d5b31f034d3b72bdc27a41"; logging-data="7620"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/5z31CXZWt1aJYCLYYEsZn" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:95AR8dnhDykn/GH5cCf5/AnG+hA= sha1:wmsRMOp0JxjZK67Fl2Zz8sFoYbc= Xref: csiph.com comp.lang.python:79367 Dan Stromberg : > Often with TCP protocols, line buffered is preferred to character > buffered, Terminal devices support line buffering on write. Line buffering on read is an illusion created by higher-level libraries. The low-level read function reads in blocks of bytes. > 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. Also, Nagle doesn't play any role with pipes. >> ======================================================================== >> $ 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. 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. Marko