from decorator import decorator
import sqlalchemy
class Data:
"""A very simple class to save the thread pool for database requests"""
def __init__(self):
self.pool = None
data = Data()
"""holds connection to the actual thread pool for the database requests"""
def setPool(pool):
"""setting the thread pool"""
data.pool = pool
@decorator
def runInDBPool(f,*args,**kwargs):
"""Decorator to run DB queries in Twisted's thread pool.
If last argument is a session object, the function is called directly.
"""
if isinstance(args[-1],sqlalchemy.orm.session.Session):
return f(*args,**kwargs)
else:
return data.pool.run(f, *args, **kwargs)