-
-
Save dmpeters/011edab4b416d5f93c4c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Backend(object): | |
def __init__(self): | |
engine = create_engine("mysql://{0}:{1}@{2}/{3}".format(options.mysql_user, options.mysql_pass, options.mysql_host, options.mysql_db) | |
, pool_size = options.mysql_poolsize | |
, pool_recycle = 3600 | |
, echo=options.debug | |
, echo_pool=options.debug) | |
self._session = sessionmaker(bind=engine) | |
@classmethod | |
def instance(cls): | |
"""Singleton like accessor to instantiate backend object""" | |
if not hasattr(cls,"_instance"): | |
cls._instance = cls() | |
return cls._instance | |
def get_session(self): | |
return self._session() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BaseHandler(tornado.web.RequestHandler): | |
@property | |
def backend(self): | |
return Backend.instance() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ExampleHandler(BaseHandler): | |
def get(self): | |
session = self.backend.get_session() | |
try: | |
"""Everything I need to do here""" | |
session.commit() | |
except Exception as e: | |
session.rollback() | |
finally: | |
session.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment