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

Grant Edwards ge at nowhere.none
Tue Oct 3 11:58:45 EDT 2000


In article <8FC2A1B30mikecat at 192.168.10.38>, Mike 'Cat' Perkonigg wrote:

>There comes another question in mind: Can I somehow tell a
>thread that there is nothing to do so another thread can
>consume more time?

I haven't done any threaded stuff in Python, but from what I've
read it looks like fairly generic threading support, so I'll
give some general multi-threading advice...

What you need to do is design the program so that the thread is
blocked when it has nothing more to do.  Then you don't have to
"tell" it.  It just stops and other stuff runs. I always have
each tread block on something: a timer, semaphore, socket,
mailbox, data-queue, hardware-interrupt, etc. until there's
something for it to do.

If a thread is "consuming" something (e.g. a TCP data stream,
Ethernet packets, serial data, commands from another thread)
then figure out how to make the thread block when there's
nothing left to consume.

If the thread just needs to run periodically, then have it
block on a timer that fires once every X ms and wakes up the
thread.

If you've got a thread that isn't strictly cyclical or a
consumer, but a mixture, you can generally do a
wait-with-timeout on whatever resource it is consuming.

>I heard the active thread changes if there are 10 byteOps are
>done. Does that mean I have just to implement a "while 1: pass"
>loop to have this thread consume less time?


-- 
Grant Edwards                   grante             Yow!  Yow!
                                  at               
                               visi.com            



More information about the Python-list mailing list