[Twisted-Python] recommended twisted database coding

Thomas HERVE therve at free.fr
Fri Jun 9 10:33:13 EDT 2006


Quoting Thomas Vander Stichele <thomas at apestaart.org>:

> Hi everyone,

Hi,

> - Twisted is, of course, async, so twisted.enterprise.adbapi wraps 2.0
> DB API's using threads, which makes sense.  Are people using adbapi much
> in production though ?

Here we use adbapi a lot, against Postgresql, MySQL and SQLite. Note 
that adapi is mainly a wrapper around a threadpool, there's very little 
magic in it (which explains it works great).

> From scanning the mailing list it seems like
> people prefer layering stuff on top of adbapi to implement object
> models.

We did that too :). I just modeled a simple ORM to construct objects 
from SQL results (it just put the values into attributes of a dict or 
an object).

We also use a simple wrapper around ConnectionPool to handle specific 
databases problems (concurrent updates in Postgres or timeouts in 
MySQL), and add other methods (for example executemany of pysqlite2 is 
very useful).

> - SQLObject seems to come very close to what I would want to use to
> abstract away the database backend, and just pretend that I only deal
> with persistently stored python objects.  Has anyone managed to use
> SQLObject in a twisted project ? If not, what's the closest I can get
> that gives me the advantage of having a python object backend ?

I strongly discourage you from using SQLObject. It's really a pain to 
use, and there's implicit behaviours that are nasty (cache, attributes 
retrieval...). It's not very hard to use with a thread pool in Twisted, 
but I think it's really designed for simple threaded (web) apps.

SQLAlchemy seems much cleaner (but still in early stage) and as advised 
by Ed Suominen you should take a look at sAsync.

-- 
Thomas

----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.





More information about the Twisted-Python mailing list