[Twisted-Python] Simplifying database access in Twisted

Bob Ippolito bob at redivi.com
Thu Jun 19 16:24:33 MDT 2003


On Thursday, Jun 19, 2003, at 17:13 America/New_York, Thomas Weholt 
wrote:

> Just read thru http://www.twistedmatrix.com/documents/howto/enterprise 
> and
> tried to fit the code to my needs, but ... this seems way too 
> complicated.
> All I want is to execute a SQL-query and do something similar to 
> fetchone or
> fetchall etc. in the DB-API 2.0 spec. I don't want to be forced to 
> write
> classes and callbacks etc. just for a simple list of tuples or 
> something
> returned from a SQL-query.
>
> Is there no other way to do simple database access in Twisted? 
> Something
> more like the DB-API 2.0? The docs says "This is straightforward ...". 
> No,
> it's not. The example a bit further up on the same page, now that's
> straightforward. Perhaps it's just me not getting all the callbacks and
> whatnot, but this seems too hard, too much code for such a simple 
> task. All
> I want is a database-object to feed sql-statements, which returns the
> results as dictionaries or list of tuples, as close to DB-API 2.0 as
> possible. Any hint or clue would be highly appreciated.
>
> PS! I don't mean to step on anyones toes or anything, so forgive me if 
> I'm a
> bit harsh. But this might be the bitter end of my journey into the 
> realms of
> Twisted. Quick, fast and easy access to a database will make-or-break 
> all of
> my current projects, all of which are based on Twisted.

Here are some doc-examplets not using Augmentation.

from twisted.enterprise import adbapi

noop = lambda x:x
cp = adbapi.ConnectionPool('pyPgSQL.PgSQL', '::yourdatabase::')

deferred = cp.interaction(yourcallback, noop, *yourcallbackargs, 
**yourcallbackkwargs)
#	pass in a callback that receives a cursor as its first argument, 
DBAPI your little heart out.#
#	returns whatever you return in your callback.
#	note that 'yourcallback' does not run in the main reactor thread!  be 
careful not to try and call into it directly from here.

deferred = cp.operation(noop, noop, *executeargs, **executekwargs)
#	does cursor.execute(*executeargs, **executekwargs)
#	returns a deferred that calls back with None.  It fetches no data at 
all, useful for insert/update/stored procedure type stuff.

deferred = cp.query(noop, noop, *executeargs, **executekwargs)
#	does cursor.execute(*executeargs, **executekwargs)
#	returns a deferred that calls back with the fetchall()

noop is used to make these things act more like newer Twisted 
interfaces that were designed with Deferred in mind.

Presumably if all of your current projects are based on Twisted, you're 
comfortable enough with Deferred, so I won't go into that.

-bo





More information about the Twisted-Python mailing list