Tornado + SQLAlchemy
#open-source , #programming , #python , #sqlalchemy , #tornado
Tornado is an asynchronous web framework, allowing you to handle web requests asynchronously out of the box. SQLAlchemy is an excellent database toolkit. I've been using it for close to 5 years now and not once have I had a bad experience with it.
Given the nature of both frameworks, it can be either really difficult to integrate the two, or really easy. There's no middle ground. You either need to roll up your sleeves and go full-async, or you can make a few assumptions about your application and write a small set of helper functions that assume the user knows the two frameworks.
I'm lazy, so I chose the second option and wrote
tornado-sqlalchemy. It's a
small module that tries to make SQLAlchemy usage in Tornado applications a bit
If you're currently using both the frameworks in your application, you probably wrote your own boilerplate code that establishes the database connection, initializes the engine and then finally obtains the session. In addition, you probably also wrote code to make sure that the session lifetime is the same as the request lifetime.
Similarly, if you also have alembic in your stack, that means you likely have
some custom code written so that
alembic has the correct MetaData object.
tornado-sqlalchemy tries to address these issues by making one big fat
assumption - if you're using SQLAlchemy and Tornado, you know how they work
internally. This means, you should know how that ioloop works, how
session handling works, and how to work with connection and engine objects.
This results in a standardized library which can act as a central place for
bugs features, and hopefully can establish best practices.
Anyway, the module is on PyPI under the name
tornado-sqlalchemy, and the
source is on Github. Hope it's useful!