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: Mon, 20 Oct 2014 07:45:22 +0300 Organization: A noiseless patient Spider Lines: 25 Message-ID: <87a94rgxi5.fsf@elektro.pacujo.net> References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="ff5cf27ef3d5b31f034d3b72bdc27a41"; logging-data="19775"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18+/FbqowcQJP41nf6SKrwp" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:PM7q39ObIGoupimUl9+gq9TrH2E= sha1:JCzDHC8WGKWIc8FNA5VFCkqUJyk= Xref: csiph.com comp.lang.python:79303 Cameron Simpson : > Even if nc itself does no buffering (handing data to the OS as soon as > received, highly desirable for a tool like nc), the OS keeps a buffer > for the pipeline between nc and python, Yes, there is a buffer associated with the pipe, but linux/unix never withholds any data from the reader. As soon as there is a single byte in the pipe buffer, the reader process becomes ready to run and read(2) on the pipe returns immediately. > and python itself keeps a buffer for sys.stdin. I found this comment in CPython's source code (pythonrun.c): /* stdin is always opened in buffered mode, first because it shouldn't make a difference in common use cases, second because TextIOWrapper depends on the presence of a read1() method which only exists on buffered streams. */ The solution is to use os.read(). Marko