[Twisted-Python] dbapi and pb question

Fredrik info at cleosan.com
Thu Aug 1 06:21:59 EDT 2002


I am a newbie to this  and i try to connect a database to pb,
when trying this code i dont get any response from the server (running pbgtk.py)but the server outputs the print statement, how do I send bacdk a callback to the client ??



from twisted.enterprise import adbapi 
from twisted.spread import pb
from twisted.internet import app


class AgeDatabase(adbapi.Augmentation): 
    """A simple example that can retrieve an age from the database""" 

    def getAAge(self): 
        sql = """SELECT age FROM person """ 
        return self.runQuery(sql) 
 
    def getAge(self, name): 
        sql = """SELECT age FROM person where name = %s """ 
        return self.runQuery(sql, name) 
 
class DefinedError(pb.Error):
    pass

class SimplePerspective(pb.Perspective):
    db = AgeDatabase(adbapi.ConnectionPool("MySQLdb", db='test')) 

    def perspective_echo(self, text):
        print 'echoing',text
        self.db.getAge(text).addCallback(self.gotAge,text)  

    def perspective_error(self):
        raise DefinedError("exception!")

    #########################################
    # the next function wont return to client 
    
    def gotAge(self,resultlist, name): 
        age = resultlist[0][0]          # First field of first record 
        print "%s is %d years old" % (name, age) 
        return str(age) 


class SimpleService(pb.Service):
    def getPerspectiveNamed(self, name):
        p = SimplePerspective(name)
        p.setService(self)
        return p

if __name__ == '__main__':
    import pbecho
    appl = app.Application("pbecho")
    pbecho.SimpleService("pbecho",appl).getPerspectiveNamed("guest").makeIdentity("guest")
    appl.listenTCP(pb.portno, pb.BrokerFactory(pb.AuthRoot(appl)))
    appl.run()
    #appl.save("start")



Fredrik Bjornsson
info at cleosan.com





More information about the Twisted-Python mailing list