[bind10-dev] ASIO and asyncore, was Re: Question about config channel and another msgq channel

Shane Kerr shane at isc.org
Tue Jun 22 14:44:51 UTC 2010


On Tue, 2010-06-22 at 15:52 +0800, zhanglikun wrote:
> > 
> > Is there a good asynchronous/event driven library in python ?
> > (compatible with boot::asio)
> 
> Asyncore?

asyncore is similar, but not quite the same. asyncore calls you when
events happen rather than when data is ready for processing.

So, in ASIO you may get an event when a certain amount of data has been
read. For TCP this works great... on TCP you read the length of the
message (4 bytes) then get a callback, then you begin a read of the
entire packet (LEN bytes) and get a callback.

In asyncore you get an event when data is ready. So you then have to try
a read() of the amount of data you want, and if you get enough to
complete your operation then you proceed. So it would look more like:

        def handle_read(self):
        	if len(self.len_buf) < 4:
        		buf = self.recv(4 - len(self.len_buf))
        		self.len_buf = self.len_buf + buf
        		(self.len,) = struct.unpack('!L', self.len_buf)
        	if len(self.len_buf) == 4:
        		buf = self.recv(self.len - len(self.message_buf))  

(I'm not sure about the EOF case and suchlike here.)

Anyway, asyncore is good (the popular Twisted framework is built on it),
but it is not the same as ASIO. It is simpler I think, but may involve a
bit more work on the application side.

--
Shane




More information about the bind10-dev mailing list