Python helpers for using SQLAlchemy with Tornado.
$ pip install tornado-sqlalchemyIn case you prefer installing from the Github repository, please note that master is the development branch so stable is what you should be installing from.
fromtornado.genimportcoroutinefromtornado.webimportApplication, RequestHandlerfromtornado_sqlalchemyimportas_future, SessionMixin, SQLAlchemyclassNativeCoroutinesRequestHandler(SessionMixin, RequestHandler): asyncdefget(self): withself.make_session() assession: count=awaitas_future(session.query(User).count) self.write('{} users so far!'.format(count)) classGenCoroutinesRequestHandler(SessionMixin, RequestHandler): @coroutinedefget(self): withself.make_session() assession: count=yieldas_future(session.query(User).count) self.write('{} users so far!'.format(count)) classSynchronousRequestHandler(SessionMixin, RequestHandler): defget(self): withself.make_session() assession: count=session.query(User).count() self.write('{} users so far!'.format(count)) handlers= ( (r'/native-coroutines', NativeCoroutinesRequestHandler), (r'/gen-coroutines', GenCoroutinesRequestHandler), (r'/sync', SynchronousRequestHandler), ) app=Application( handlers, db=SQLAlchemy('postgres://user:password@host/database') )Documentation is available at Read The Docs.
Please make sure you have Python 3.5+ and Poetry installed.
Since we run tests against multiple databases (currently MySQL, PostgreSQL, and SQLite), we use docker-compose to make our lives easier.
- Git clone the repository -
git clone https://github.com/siddhantgoel/tornado-sqlalchemy - Install the packages required for development -
poetry install - Ensure that the MySQL and PostgreSQL services (containers) are up -
docker-compose up -d - That should basically be it. You should now be able to run the test suite -
poetry run py.test tests/.