[Twisted-Python] using defers with requests

charles brandt twisted at st.imul.us
Sat Nov 15 21:57:38 EST 2003


I am trying to select some data from a database in hopes of eventually using
it to populate a dynamic page.  Before I dive into woven, I was trying to
get the following to print selected data directly to a html resource using
the following code snippet (or various versions of it).   I've only been
able to get an empty page though ( <html><body></body></html> ).  I'm
wondering if maybe the request is getting returned before the callback makes
it to the defer?  Any ideas? Thanks in advance,

-Charles.

from twisted.web import resource, server
from twisted.enterprise import adbapi

dbpool = adbapi.ConnectionPool(<connection stuff>)

def getData(data):
    return dbpool.runQuery("SELECT * FROM some_table WHERE some_column =
%s", data)

def printResult(data, request):
    request.write("pre-data")
    request.write(data)
    request.write("post-data")
    request.finish()

class myResource(resource.Resource):
    def render(self, request):
        d = getData("some string")
        d.addCallback(printResult, request)
        return server.NOT_DONE_YET

resource = myResource()






More information about the Twisted-Python mailing list