[Twisted-Python] dbapi and pb question

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@cleosan.com

looks to me like the method "perspective_echo" should return the defered that the method "getAge" gets from the dbapi.
Try changing:
def perspective_echo(self, text): print 'echoing',text self.db.getAge(text).addCallback(self.gotAge,text)
to
def perspective_echo(self, text): print 'echoing',text return self.db.getAge(text).addCallback(self.gotAge,text)
-----Original Message----- From: twisted-python-admin@twistedmatrix.com [mailto:twisted-python-admin@twistedmatrix.com]On Behalf Of Fredrik Sent: Thursday, August 01, 2002 5:22 AM To: twisted-python@twistedmatrix.com Subject: [Twisted-Python] dbapi and pb question
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").makeIdentit y("guest") appl.listenTCP(pb.portno, pb.BrokerFactory(pb.AuthRoot(appl))) appl.run() #appl.save("start")
Fredrik Bjornsson info@cleosan.com
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

Thank you it works and now i will try to make many sqls in one transaction, if I have any problem I will return ...
Regards Fredrik
On Fri, Aug 02, 2002 at 11:31:23PM -0500, Sean Riley wrote:
looks to me like the method "perspective_echo" should return the defered that the method "getAge" gets from the dbapi.
Try changing:
def perspective_echo(self, text): print 'echoing',text self.db.getAge(text).addCallback(self.gotAge,text)
to
def perspective_echo(self, text): print 'echoing',text return self.db.getAge(text).addCallback(self.gotAge,text)
-----Original Message----- From: twisted-python-admin@twistedmatrix.com [mailto:twisted-python-admin@twistedmatrix.com]On Behalf Of Fredrik Sent: Thursday, August 01, 2002 5:22 AM To: twisted-python@twistedmatrix.com Subject: [Twisted-Python] dbapi and pb question
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").makeIdentit y("guest") appl.listenTCP(pb.portno, pb.BrokerFactory(pb.AuthRoot(appl))) appl.run() #appl.save("start")
Fredrik Bjornsson info@cleosan.com
Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
participants (2)
-
Fredrik
-
Sean Riley