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

Grant Edwards ge at nowhere.none
Tue Oct 3 16:23:18 CEST 2000


In article <39d9df0c_1 at corp.newsfeeds.com>, Joshua Muskovitz wrote:

>> Why is the socket set to non-blocking?  Why not set it to
>> blocking so that the thread blocks until there's data to read?
>
>This is one of those "um" questions.  The answer is "um, well,
>the prototype code I was given to start with set it to
>non-blocking." <insert sheepish grin here>
>
>The only reason I can thing of is that a separate thread needs
>to be able to use the same socket to send requests (send and
>recv operate on the same socket in different threads), but this
>may not be a valid reason either.

I _think_ that you can set the thread to blocking, and then the
receive thread will sleep when it has nothing to do.  The send
threads should still be fine, unless they are currently
returning EAGAIN or EWOULDBLOCK errors -- in which case the
blocking socket will cause the sending threads will block until
the send operation can complete.

>select.select worked great, but now I've got other problems,
>namely figureing out why I'm no longer catching the
>KeyboardInterrupt exception I used to be able to catch.

Don't know for sure about that one. 

-- 
Grant Edwards                   grante             Yow!  Let's climb to the
                                  at               TOP of that MOUNTAIN and
                               visi.com            think about STRIP MINING!!



More information about the Python-list mailing list