Unexpected (?) Thread behaviour

Jordan Krushen jordan at krushen.com
Mon May 5 20:32:51 EDT 2003


On Fri, 02 May 2003 23:59:44 -0400, Peter Hansen <peter at engcorp.com> wrote:

> Vincent Berg wrote:
>> while 1:
>> if len(self.clients) > 0:
>> rd, wd, ex = select.select(self.clients, [], [], None)
>> for n in rd:
>> n.handle()

> Among other things, this is not what you want to do....
>
> The above while loop will do a "busy-wait", consuming all the available
> CPU time (except for when Python switches to another thread) waiting
> for self.clients to get something in it.
>
> At least put a "time.sleep(0.005)" or something after the if statement.

Isn't that what the timeout is for in select.select?  (That would be the 
'None' param in the select call above).  Putting time.sleep in the loop 
seems somewhat inelegant - if there *are* clients to serve, shouldn't we be 
doing that at full speed, instead of waiting?  If FDs are ready, select 
will return them right away.  If not, it'll wait a while, if given a 
timeout.

rd, wd, ex = select.select(self.clients, [], [], 1)
                                                 ^

J.




More information about the Python-list mailing list