[Twisted-Python] Using sqlalchemy in twisted.

Peter Cai newptcai at gmail.com
Thu Mar 5 00:38:43 MST 2009


I changed my code to this style, it works again.

This code would create a new session on each request and close it
immediately.

Hope this trick could save me!

#=========================================

def require_session(f):
    '''create and close session for each synchronous method'''
    def wrapper(model, *args, **kw):
        sess = model.Session()
        try:
            return f(model, sess, *args, **kw)
        finally:
            sess.close()
    return wrapper

class Database()
    def __init__(self, conn_str):
        self.conn_str = conn_str
        self.engine = create_engine(self.conn_str, echo=False)
        self.Session = sessionmaker(bind =
self.engine,  expire_on_commit=False)

    def getObjectById(self, klass, id):
        return threads.deferToThread(self._getObjectById, klass, id)

    @require_session

    def _getObjectById(self, sess, klass, id):

        return sess.query(klass).get(id)
#=========================================

On Thu, Mar 5, 2009 at 4:29 AM, Valentino Volonghi <dialtone at gmail.com>wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
>
> On Mar 4, 2009, at 11:15 AM, Chris Foster wrote:
>
>  I think SQLAlchemy's ORM might work fine with Twisted.  Check out
>> http://foss.eepatents.com/sAsync/ .  sAsync doesn't appear to be widely
>> used, but I got the examples to run with some minor changes to the sqlite
>> connection.  I'm hoping to try something useful in the next week or two.
>>
>
>
> sAsync repository hasn't changed since about 1 or 2 years ago. Now
> sqlalchemy 0.5
> changed quite a bit of the internals and so on. Notice however that the
> tests for
> sAsync use sqlite that is limited essentially to 1 connection, which goes
> back to
> your original discontent.
>
> I tried to integrate sqlalchemy ORM in the past and it never worked right
> because
> the code in the objects didn't know that it was dealing with twisted and
> because
> sqlalchemy is mainly used in a single thread, it's thread safe but that
> doesn't mean
> that you can share objects between threads.
>
> - --
> Valentino Volonghi aka Dialtone
> Now running MacOS X 10.5
> Home Page: http://www.twisted.it
> http://www.adroll.com
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.9 (Darwin)
>
> iEYEARECAAYFAkmu5KoACgkQ9Llz28widGWBWwCglXIKPUFaGbk5tI9XmtrH8lH+
> 7woAnjrWWWEj2P78szZbNyLzSx0kgz4B
> =9uZo
>
> -----END PGP SIGNATURE-----
>
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>



-- 
look to the things around you,the immediate world around you, if you are
alive,it will mean something to you ——Paul Strand
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20090305/c51d3772/attachment.html>


More information about the Twisted-Python mailing list