[Twisted-Python] dbapi and pb question
Sean Riley
sean at twistedmatrix.com
Sat Aug 3 00:31:23 EDT 2002
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 at twistedmatrix.com
[mailto:twisted-python-admin at twistedmatrix.com]On Behalf Of Fredrik
Sent: Thursday, August 01, 2002 5:22 AM
To: twisted-python at 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 at cleosan.com
_______________________________________________
Twisted-Python mailing list
Twisted-Python at twistedmatrix.com
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
More information about the Twisted-Python
mailing list