Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Nobody Newsgroups: comp.lang.python Subject: Re: Flush stdin Date: Sun, 19 Oct 2014 02:11:56 +0100 Organization: A noiseless patient Spider Lines: 29 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Info: mx02.eternal-september.org; posting-host="152d4dee6f95183c956ee6fa63d7e69f"; logging-data="24157"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/3YjKRF2VkkmfD7Vw+29IL" User-Agent: Pan/0.14.2 (This is not a psychotic episode. It's a cleansing moment of clarity.) Cancel-Lock: sha1:cznU6eJBO6wqwLM4HzcP3Hveh4E= Xref: csiph.com comp.lang.python:79281 On Sat, 18 Oct 2014 12:32:07 -0500, Tim Chase wrote: > On 2014-10-18 17:55, Nobody wrote: >> On Fri, 17 Oct 2014 12:38:54 +0100, Empty Account wrote: >> >> > I am using netcat to listen to a port and python to read stdin and >> > print to the console. >> > >> > nc -l 2003 | python print_metrics.py >> > >> > sys.stdin.flush() doesn’t seem to flush stdin, >> >> You can't "flush" an input stream. > > You can't flush it, but you can make it unbuffered. You can either force > python to use unbuffered stdio: [snipped] None of this helps in any way, as it's not the behaviour of the Python script which is causing the problem, but that "nc" is (probably) buffering its output, so the data isn't passed to the OS (let alone to the Python script) in a timely manner. Once the "nc" process actually write()s the data to its standard output (i.e. desriptor 1, not the "stdout" FILE*), it will be available to the Python script immediately thereafter without requiring any low-level tweaks.