Select in Python

Gary Herron gherron at islandtraining.com
Tue Jun 27 02:19:04 EDT 2006


Dio wrote:
> #!/usr/bin/env python2
>
> from sys import stdin
> from select import select
>
> while 1:
>     (rr, wr, er) = select([stdin], [], [])
>     for fd in rr:
>         print fd
>
> program block in the first select(), after I type something and "enter
> ", it never block in select() again,why?
>
>   
Because select blocks until there is something to read on stdin, and as
long as there is something to be read on stdin, it will not block. Since
your program never reads in the data on stdin, your "enter\n" just sits
there waiting to be read, and select always inform you of that fact by
returning stdin in the rr list.

If you want select to start blocking again, you must read all bytes from
stdin whenever select says there are bytes to be read.

Gary Herron




More information about the Python-list mailing list