[Twisted-Python] design question

Pet petshmidt at googlemail.com
Mon May 4 07:04:53 EDT 2009


I've written my first Twisted Server, which accept request from
clients, does DB queries, sends response back and logs some events in
DB. I wonder, if my design was good.
Basically,  I have a Factory where is db connectionPool created and
Protocol(basic.LineReceiver), which processes requests and sends data
In Protocol, I instantiate my utility object which I try to keep
independent from twisted framework, so it can be easily used
somewhere else or can be replaced by other class.

def m(self, *args, **kwargs ):
           d = self.getData(params=kwargs['params'])
           if d:

               return None
       except Exception, e:
           log.msg('Unknown error in m:', e)

def getData(self, *args, **kwargs):
   obj = MyObj(self.db)
   return self.db.runInteraction(obj.getDataFromDB,kwargs['parameter'])

What I don't like, I must pass db variable to obj.getDataFromDb and
may be other function called there. I think it would be better obj =
MyObj(self.db), but it doesn't work and runInteraction passes
automatically db variable to interaction method.

I'm new to Python and Twisted and would appreciate your advises.

More information about the Twisted-Python mailing list