[Twisted-Python] using defers with requests

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()

On Sat, Nov 15, 2003 at 06:57:38PM -0800, charles brandt wrote:
Are you sure your code is even being triggered? Since your code isn't writing out those HTML tags anywhere, it seems like some completely different resource is being renderred instead of yours. Can you put some debug prints in "render" and "printResult"? -- Twisted | Christopher Armstrong: International Man of Twistery Radix | Release Manager, Twisted Project ---------+ http://radix.twistedmatrix.com/

Yes, I think Mozilla is the source for the "<html><body></body></html>" string. I don't think anything is being sent. If I put a request.finish() in "render" then the resource returns any data written to request in "render" (but not "printResult"). I'm guessing that the connection with the client gets closed before a request is rendered. Is anyone aware of any examples of web applications written in twisted that uses a database to store dynamic elements? Thats ultimately where I'm trying to get with this. Thanks for the responses. -Charles. I added some print debug statements as follows: def printResult(data, request): print "entered printResult" request.write("pre-data") request.write(data) request.write("post-data") request.finish() print request return server.NOT_DONE_YET class myResource(resource.Resource): def render(self, request): print "entered render" d = getData("some string") d.addCallback(printResult, request) print "after callback" return server.NOT_DONE_YET resource = myResource() which results in the following logs being generated: 2003/11/15 19:49 PST [HTTPChannel,13,<ip>] entered render 2003/11/15 19:49 PST [HTTPChannel,13,<ip>] after callback 2003/11/15 19:49 PST [HTTPChannel,13,<ip>] adbapi connecting: MySQLdb {<connection args>} 2003/11/15 19:49 PST [-] entered printResult 2003/11/15 19:49 PST [-] <ip> - - [16/Nov/2003:03:49:11 +0000] "GET /beta/resource3.rpy HTTP/1.1" 200 18 "-" "Mozilla/5.0" 2003/11/15 19:49 PST [-] <GET /beta/resource3.rpy HTTP/1.1>

On Sat, Nov 15, 2003 at 07:59:05PM -0800, charles brandt wrote:
What is 'data' here? can you print repr(data)? It might be the empty string, or something.
return server.NOT_DONE_YET
This is unnecessary here, FYI.
-- Twisted | Christopher Armstrong: International Man of Twistery Radix | Release Manager, Twisted Project ---------+ http://radix.twistedmatrix.com/

On Sat, Nov 15, 2003 at 06:57:38PM -0800, charles brandt wrote:
Are you sure your code is even being triggered? Since your code isn't writing out those HTML tags anywhere, it seems like some completely different resource is being renderred instead of yours. Can you put some debug prints in "render" and "printResult"? -- Twisted | Christopher Armstrong: International Man of Twistery Radix | Release Manager, Twisted Project ---------+ http://radix.twistedmatrix.com/

Yes, I think Mozilla is the source for the "<html><body></body></html>" string. I don't think anything is being sent. If I put a request.finish() in "render" then the resource returns any data written to request in "render" (but not "printResult"). I'm guessing that the connection with the client gets closed before a request is rendered. Is anyone aware of any examples of web applications written in twisted that uses a database to store dynamic elements? Thats ultimately where I'm trying to get with this. Thanks for the responses. -Charles. I added some print debug statements as follows: def printResult(data, request): print "entered printResult" request.write("pre-data") request.write(data) request.write("post-data") request.finish() print request return server.NOT_DONE_YET class myResource(resource.Resource): def render(self, request): print "entered render" d = getData("some string") d.addCallback(printResult, request) print "after callback" return server.NOT_DONE_YET resource = myResource() which results in the following logs being generated: 2003/11/15 19:49 PST [HTTPChannel,13,<ip>] entered render 2003/11/15 19:49 PST [HTTPChannel,13,<ip>] after callback 2003/11/15 19:49 PST [HTTPChannel,13,<ip>] adbapi connecting: MySQLdb {<connection args>} 2003/11/15 19:49 PST [-] entered printResult 2003/11/15 19:49 PST [-] <ip> - - [16/Nov/2003:03:49:11 +0000] "GET /beta/resource3.rpy HTTP/1.1" 200 18 "-" "Mozilla/5.0" 2003/11/15 19:49 PST [-] <GET /beta/resource3.rpy HTTP/1.1>

On Sat, Nov 15, 2003 at 07:59:05PM -0800, charles brandt wrote:
What is 'data' here? can you print repr(data)? It might be the empty string, or something.
return server.NOT_DONE_YET
This is unnecessary here, FYI.
-- Twisted | Christopher Armstrong: International Man of Twistery Radix | Release Manager, Twisted Project ---------+ http://radix.twistedmatrix.com/
participants (3)
-
Andrew Bennetts
-
charles brandt
-
Christopher Armstrong