iro/model/pool.py
author Sandro Knauß <knauss@netzguerilla.net>
Thu, 29 Mar 2012 17:21:46 +0200
branchdevel
changeset 259 5d9c24c2cb8d
parent 258 0a5eb5aac0be
child 294 0e75bd39767d
permissions -rw-r--r--
iro.model.utils: adding docstrings

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)