[Twisted-Python] Triggering callbacks on raw file descriptors

Jean-Paul Calderone exarkun at divmod.com
Sat Jan 7 14:04:09 EST 2006


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




More information about the Twisted-Python mailing list