Mailing List Archive

asyncore 100% cpu usage?
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?

any help is greatly appreciated.

Adonis

-- paste --

class PYCHSCK(asyncore.dispatcher):

def __init__(self, host, port):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.connect((host, port))

def handle_connect(self):
print 'Connected'
self.send('NICK foobar\r\n')
self.send('USER foo "HOME" "irc.prison.net" :bar.\r\n')

def handle_read(self):
data = self.recv(8192)
print data

def handle_write(self):
pass

def handle_close(self):
self.close()

def pychStartSock(ip, port):
pych = PYCHSCK(ip, port)
asyncore.loop()

if __name__ == '__main__':
# not a pretty way for threading but just for
# quick test purposes.
threading.thread(target=pychStartSock, args=('x.x.x.x', 6667)).start()
asyncore 100% cpu usage? [ In reply to ]
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,
Dave