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


Groups > comp.lang.python > #79283

Re: Flush stdin

References <mailman.14954.1413612650.18130.python-list@python.org> <pan.2014.10.18.16.55.19.164000@nowhere.invalid> <mailman.14970.1413653368.18130.python-list@python.org> <pan.2014.10.19.01.11.55.240000@nowhere.invalid>
Date 2014-10-18 18:34 -0700
Subject Re: Flush stdin
From Dan Stromberg <drsalists@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.14977.1413682500.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Sat, Oct 18, 2014 at 6:11 PM, Nobody <nobody@nowhere.invalid> wrote:
> 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.

Agreed.

> Once the "nc" process actually write()s the data to its standard
> output (i.e. desriptor 1, not the "stdout" FILE*)
I'm not sure why you're excluding stdout, but even if nc is using
filedes 1 instead of FILE * stdout, isn't it kind of irrelevant?

> it will be available to
> the Python script immediately thereafter without requiring any low-level
> tweaks.
Which, on a pipe, generally means either the buffer filled and needed
to be passed along to make room, or the process exited.

I'd probably rewrite just enough nc in Python to make it so you don't
need to depend on a pipe (example:
http://stromberg.dnsalias.org/~strombrg/pnetcat.html), but if you're
on *ix you could try
http://ftp.sunet.se/pub/usenet/ftp.uu.net/comp.sources.unix/volume23/pty/
in an effort to persuade nc to think that it's on a tty and hence
should output line buffered data instead of block buffered - despite
being on a pipe in reality.

HTH.

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


Thread

Flush stdin Empty Account <emptya45@gmail.com> - 2014-10-17 12:38 +0100
  Re: Flush stdin Nobody <nobody@nowhere.invalid> - 2014-10-18 17:55 +0100
    Re: Flush stdin Tim Chase <python.list@tim.thechases.com> - 2014-10-18 12:32 -0500
      Re: Flush stdin Nobody <nobody@nowhere.invalid> - 2014-10-19 02:11 +0100
        Re: Flush stdin Dan Stromberg <drsalists@gmail.com> - 2014-10-18 18:34 -0700
        Re: Flush stdin Dan Stromberg <drsalists@gmail.com> - 2014-10-18 18:42 -0700
          Re: Flush stdin Nobody <nobody@nowhere.invalid> - 2014-10-22 03:49 +0100
            Re: Flush stdin Dan Stromberg <drsalists@gmail.com> - 2014-10-21 20:11 -0700
    Re: Flush stdin MRAB <python@mrabarnett.plus.com> - 2014-10-18 18:55 +0100
    Re: Flush stdin Cameron Simpson <cs@zip.com.au> - 2014-10-19 08:01 +1100
    Re: Flush stdin Terry Reedy <tjreedy@udel.edu> - 2014-10-18 18:35 -0400

csiph-web