how do I listen on a socket without sucking up all the CPU time?

Grant Edwards ge at nowhere.none
Fri Oct 13 12:09:12 EDT 2000


In article <slrn8ucctb.eam.kc5tja at garnet.armored.net>, Samuel A. Falvo II wrote:

>>Using select with a timeout of NULL(None) is the nicer way to
>>do things, i.e. make select blocking.  It means this thread
>>doesn't have to be limited to just that socket.  It obviously
>>becomes a lot more useful.
>
>I've read the select() source in Linux's source code.  From
>what I can see, it apparently busy-waits on the selectors you
>give it -- I don't see any calls to functions that put the
>process to sleep.

>If I'm wrong on this, and I sure hope I am, could someone
>please point out where Linux does put the calling process to
>sleep?

You're wrong on this. ;)

If you look at the top of the for (;;) loop in do_select()
[fs/select.c] the task state gets set to TASK_INTERRUPTIBLE.

That means we're not in a runnable state. At the bottom of the
loop there's a call to schedule_timeout(). Inside
schedule_timeout() the scheduler gets called which gives up the
processor, and that task won't get scheduled again until the
timer times out or some event/signal happens.  At that point
the do_select()'s call to schedule_timout() returns.

When the stuff in the middle of for (;;) loop decides something
has_ happened that we care about (by incrementing retval), the
code at the bottom of the loop breaks out of the loop and sets
the task state back to TASK_RUNNING, and the task starts
running again.

-- 
Grant Edwards                   grante             Yow!  I like your SNOOPY
                                  at               POSTER!!
                               visi.com            



More information about the Python-list mailing list