[Python-ideas] BufferedIO and detach
robertc at robertcollins.net
Mon Mar 4 22:17:11 CET 2013
On 4 March 2013 23:45, Antoine Pitrou <solipsis at pitrou.net> wrote:
> Le Mon, 4 Mar 2013 23:15:36 +1300,
> Robert Collins
> <robertc at robertcollins.net> a écrit :
>> On 4 March 2013 22:59, Antoine Pitrou
>> <solipsis at pitrou.net> wrote:
>> > Le Mon, 4 Mar 2013 19:44:27 +1300,
>> > Robert Collins
>> > <robertc at robertcollins.net> a
>> > écrit :
>> >> Yes exactly. A little more context on how I came to ask the
>> >> question. I wanted to accumulate all input on an arbitrary stream
>> >> within 5ms, without blocking for longer. Using raw IO + select,
>> >> its possible to loop, reading one byte at a time. The io module
>> >> doesn't have an API (that I could find) for putting an existing
>> >> stream into non-blocking mode, so reading a larger amount and
>> >> taking what is returned isn't viable.
>> > What do you mean exactly by that?
>> Just what I said. I'll happily try to rephrase. What bit was unclear?
> I don't understand what you mean by "putting an existing stream into
> non-blocking mode"? What stream exactly is it? And why is reading a
> larger amount not viable?
sys.stdin - starts in blocking mode. How do you convert it to
non-blocking mode? Portably? Now, how do you convert it to
non-blocking mode when you don't know that it is fd 1, and instead you
just have a stream (TextIOWrapper or BufferedReader or even a RawIO
If you have an fd in blocking mode, and select indicates it is
readable, reading one byte won't block. reading two bytes may block.
In non-blocking mode, reading will never block, and select tells you
whether you can expect any content at all to be available.
So reading more than one byte isn't viable when:
- the fd is in blocking mode
- you don't want to block in your program
The reason I run into this is that I have a program that deals with
both interactive and bulk traffic on the same file descriptor, and
there doesn't seem to be a portable way (where portable means
Linux/BSD/MacOSX/Windows) to flip a stream to non-blocking mode (in
Python, going by the io module docs).
Robert Collins <rbtcollins at hp.com>
HP Cloud Services
More information about the Python-ideas