from sqlalchemy import Column, BigInteger, String
from tornado.gen import coroutine
from tornado.ioloop import IOLoop
from tornado.options import options
from tornado_sqlalchemy import (declarative_base, make_session_factory,
                                SessionMixin, wrap_in_future)
from tornado.web import RequestHandler, Application


DeclarativeBase = declarative_base()


class User(DeclarativeBase):
    __tablename__ = 'users'

    id = Column(BigInteger, primary_key=True)
    username = Column(String(255), unique=True)


class SyncWebRequestHandler(RequestHandler, SessionMixin):
    def get(self):
        with self.make_session() as session:
            count = session.query(User).count()

        self.write('{} users so far!'.format(count))


class AsyncWebRequestHandler(RequestHandler, SessionMixin):
    @coroutine
    def get(self):
        with self.make_session() as session:
            count = yield wrap_in_future(session.query(User).count)

        self.write('{} users so far!'.format(count))


if __name__ == '__main__':
    session_factory = make_session_factory(options.database_url)

    Application([
        (r'/sync', SyncWebRequestHandler)
        (r'/async', AsyncWebRequestHandler)
    ], session_factory=session_factory).listen(8888)

    IOLoop.current().start()

from sqlalchemy import Column, BigInteger, String
from tornado.gen import coroutine
from tornado.ioloop import IOLoop
from tornado.options import options
from tornado_sqlalchemy import (as_future, declarative_base,
                                make_session_factory, SessionMixin)
from tornado.web import RequestHandler, Application


DeclarativeBase = declarative_base()


class User(DeclarativeBase):
    __tablename__ = 'users'

    id = Column(BigInteger, primary_key=True)
    username = Column(String(255), unique=True)


class SyncWebRequestHandler(RequestHandler, SessionMixin):
    def get(self):
        with self.make_session() as session:
            count = session.query(User).count()

        self.write('{} users so far!'.format(count))


class AsyncWebRequestHandler(RequestHandler, SessionMixin):
    @coroutine
    def get(self):
        with self.make_session() as session:
            count = yield as_future(session.query(User).count)

        self.write('{} users so far!'.format(count))


if __name__ == '__main__':
    session_factory = make_session_factory(options.database_url)

    Application([
        (r'/sync', SyncWebRequestHandler)
        (r'/async', AsyncWebRequestHandler)
    ], session_factory=session_factory).listen(8888)

    IOLoop.current().start()

update tornado example