[Twisted-Python] Triggering callbacks on raw file descriptors

Brian Granger bgranger at scu.edu
Sun Jan 8 01:46:59 EST 2006


I tried this later approach, and it worked.  I need to clean up my  
code to make it more general, but there was one problem.  I am using  
the default reactor and defined a BonjourDescriptor as follows:

class BonjourDescriptor(object):
     implements(IReadDescriptor)

     def __init__(self, fd, serviceRef):
         self.fd = fd
         self.serviceRef = serviceRef

     def fileno(self):
         return self.fd

     def doRead(self):
         ret = bonjour.DNSServiceProcessResult(self.serviceRef)


When I killed the reactor, I got an exception:

exceptions.AttributeError: 'BonjourDescriptor' object has no  
attribute 'connectionLost'

It looks like the disconnectAll method on the reactor is calling  
reader.connectionLost on each reader.  Why is this so?
I thought I only needed to implement the IReadDescriptor interface  
for my reader, but the connectionLost method is not a part of that  
interface.  Hmmm.  Should I implement it?  What interface is that  
method in?  This seems confusing.

Thanks

But the good news is that It looks like with a little work, Apple's  
zeroconf implementation can be integrated with twisted.

Brian


On Jan 7, 2006, at 11:04 AM, Jean-Paul Calderone wrote:

> On Sat, 07 Jan 2006 13:46:48 -0500, Itamar Shtull-Trauring  
> <itamar at itamarst.org> wrote:
>> On Sat, 2006-01-07 at 10:14 -0800, Brian Granger wrote:
>>
>>> Digging around the source code, it looks like the FileDescriptor  
>>> that
>>> Port inherits from would be a good starting point.  But as I
>>> understand it FileDescriptor is abstract.  Is it as simple as
>>> inheriting from FileDescriptor and providing the fd that I get from
>>> Bonjour?  How do I specify callbacks to trigger when the fd is
>>> readable/writable?
>>
>> Yep.
>>
>> You just need a fileno() method that returns the file descriptor  
>> you are
>> wrapping, and doRead and doWrite methods that they do the appropriate
>> thing (they will get called by the reactor on those events.)
>>
>
> Given the simplicity of the interface, it hardly even makes sense  
> to use FileDescriptor.  What you want can be achieved using:
>
> class BonjourThingo(object):
>    def __init__(self, fd, doRead, doWrite):
>        self.fileno = lambda: fd
>        self.doRead = doRead
>        self.doWrite = doWrite
>
> And then adding it as a reader or a writer to a reactor which  
> supports IReactorFDSet.
>
> Jean-Paul
>
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python





More information about the Twisted-Python mailing list