Re: integrating inotify into a protocol

Phil Mayers <[email protected]>
Newsgroups gmane.comp.python.twisted.web
Message-ID <[email protected]>
On 03/30/2011 02:21 AM, Jason Pepas wrote:

> another way to say this is that the protocol/client/factory side of
> things is all object-oriented, but the inotify side of things is just
> procedural.  n'ere the twain shall meet?
>
> specifically, the notifier expects the callback to accept 3 args:
> _Watch object, path, and mask.  so, when you try to pass in a class
> method as one of its callbacks, you get something like this:
>
>    exceptions.TypeError: unbound method announceNewFile() must be
> called with Echo instance as first argument (got _Watch instance
> instead)

Well, sure. You need to pass in an *instance* method, not a class method 
i.e. only once your Echo protocol class has been instantiated, do:

class Echo(LineReceiver):
   def connectionMade(self):
     inotify.whatever(self.announceNewFile, path, mask)

But if you want >1 client, you'll probably want something a bit more 
sophisticated. Specifically, you'll probably want to route the inotify 
callbacks via an instance method on the Factory, then have the Factory 
maintain a list of all Protocol instances, and fan out the inotify to 
all the clients.

e.g.

class Echo(...):
   def connectionMade(self):
     self.factory.clients.append(self)
   def connectionLost(self, ...):
     if self in self.factory.clients:
       self.factory.clients.remove(self)
   def announceNewFile(self, ...):
     whatever

class Factory(...):
   protocol = Echo

   def __init__(self):
     self.clients = []

   def announceNewFile(self, *p, **kw):
     for client in self.clients:
       client.announceNewFile(*p, **kw)


def main():
   f = Factory()
   inotify.whatever(f.announceNewFile, path, mask)
   reactor.listenTCP(port, f)
   reactor.run()
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.