[Twisted-Python] No shame...
Ken Kennedy
kkennedy at kenzoid.com
Fri Oct 25 23:03:55 MDT 2002
OK...I'm obviously not QUITE grokking something with Deferreds, and
I've spent FAR too long on something that's probably WAY too simple.
I've been trying to use a Deferred from an adbapi connection in a web
page display, and I'm just doing something wrong. I get the data, but
the page is already rendered. I know the query's working, b/c the
callback function writes to the log. At this point, I've given up and
used a blocking call just for example purposes.
Here's what's (shamefully) working now:
----BEGIN SCRIPT----
# Bloglist RTL file
#
# Provides a listing of recent blog posts
# obviously, these aren't all being used now.
# I've been through 'em all during the day, though...
from twisted.web.resource import Resource
from twisted.enterprise import adbapi
from TwistedBlog.config import DSN
from TwistedBlog import blogger
from twisted.python import log
from twisted.web.server import NOT_DONE_YET
# the heck with it...
import pyPgSQL.PgSQL
class BlogResource(Resource):
template render(self, request):
print "DSN = %s" % (DSN,)
conn = pyPgSQL.PgSQL.connect(DSN)
cursor = conn.cursor()
sql = """SELECT title, news_item_id FROM news_items
ORDER BY release_date desc, news_item_id DESC
LIMIT %s"""
cursor.execute(sql, 8)
recentList = cursor.fetchall()
"""
<html>
<head><title>Recent Entries</title></head>
<body>
<h1>Kenzoid's blog</h1>
"""
for (title, id) in recentList:
'<a href="blog/%s">%s</a><br>' % (id,title)
"""
</p>
</body>
</html>
""" % vars()
resource = BlogResource()
----END SCRIPT----
Now, TwistedBlog.blogger.DatabaseBlogger OTOH uses an
adbapi.Augmentation.runQuery with the same SQL as above, returning a
Deferred. Once the callback runs, the resultlist is available there,
and I can write it (for example) to the log. So I know it's
working. Just not in sync with the rest of things.
So I realize the problems...but I'm
just-not-seeing-the-Deferred-way. Can someone give me a dumb-stupid
example, getting basically the same info as above into a webpage,
using adbapi rather than a blocking call? I find tantalizing mentions
of the use of Deferreds in various places, but I don't see any
easy examples. I like easy. *grin* For starters, anyway.
As to format, I don't care. Use woven, Quixote PTL, Web Widgets,
direct writes, whatever floats your boat. I hope that once I see it and
play with it a tad, the lightbulb will turn on in my beany little
head.
Thanks in advance...I'll take the ribbing, as long as I get some code
too! *grin*
--
Ken Kennedy | http://www.kenzoid.com | kenzoid at io.com
More information about the Twisted-Python
mailing list