Unexpected (?) Thread behaviour

Peter Hansen peter at engcorp.com
Mon May 5 20:49:59 EDT 2003


Jordan Krushen wrote:
> 
> On Fri, 02 May 2003 23:59:44 -0400, Peter Hansen <peter at engcorp.com> wrote:
> > 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)

Definitely cleaner, though without knowing what problem the OP was 
actually facing, I didn't want to change the behaviour of his code too much...




More information about the Python-list mailing list