[Twisted-Python] design question

Pet petshmidt at googlemail.com
Mon May 4 05:04:53 MDT 2009


Hi,

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
back.
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 ):
       try:
           d = self.getData(params=kwargs['params'])
           if d:
               d.addCallback(self.sendResult)
               d.addErrback(log.err)

               d.addCallback(self.logFailedQuery)
               d.addErrback(log.err)
               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.
Pet




More information about the Twisted-Python mailing list