[Twisted-Python] Re: So how does everyone use an ORM in twisted?

David Bolen db3l.net at gmail.com
Tue Mar 17 12:06:27 EDT 2009

"Maciej Szumocki" <mszumocki at contman.pl> writes:

> I'm using SQLAlchemy with Twisted in a large project without
> problems.  Why would you think you cannot?  You just need to run SA
> operations in non-reactor threads (i currently use my own threadpool
> and deferToThread but it's only slightly modified standard twisted
> one), and you need to make sure each connection/session is not used
> by more than thread at a time, that's all.

I manage the integration very similarly but I don't use any of the ORM
features of SQLAlchemy, so to the OP's question, yes, I prefer
straight SQL.

It's much trickier if you want to use the ORM, unless you are very
careful to fully eager load every thing in any possible database
operation if you have need of the information subsequently in your
twisted code.  Otherwise you may block unexpectedly simply when
accessing your objects, and end up with database operations from the
main twisted thread.

I've never quite managed to get onto the ORM bandwagon, except for
some of my simplest applications.  But that's why I like SQLAlchemy as
much as I do, because it provides a very nice SQL layer without
requiring that you use the ORM, and if you stick with SQL, it is in
fact fairly straight forward to offload its operations to a separate
thread without blocking Twisted.

-- David

More information about the Twisted-Python mailing list