asyncore 100% cpu usage?

brueckd at brueckd at
Mon Aug 19 05:17:34 CEST 2002

On Sun, 18 Aug 2002, Adonis wrote:

> the asyncore module seems to consume 100% cpu usage, is it the way it
> suppose to be since it is nonblocking or am i flat wrong, or is it my code,
> did i miss/overlook something?

Hi Adonis, it's your code. ;-)

One minor nit: please be sure to post the actual code you use - for
example, the code below calls threading.thread (which doesn't exist)  
instead of threading.Thread.

Anyway, add a printout to handle_write and you'll see what the problem is:  
it's getting called over and over again in a tight loop. There are two
problems in the code: the first is that you need to implement your own
readable and writable methods - these signal to asyncore that you are
interested in reading and writing, respectively. The default
implementations always return 1. The second problem is that the
handle_write method has nothing more than a 'pass' statement.

Basically you need to track the state of your connection somehow so that 
you know when readable and writable should return 1. Likewise, *anytime* 
handle_read is called you better read some data, and *anytime* 
handle_write is called you better write some data. The asnychat module is 
also pretty nice for asynchronous socket work, it's slightly higher level.

Hope that helps,

More information about the Python-list mailing list