iro/model/pool.py
author Sandro Knauß <knauss@netzguerilla.net>
Wed, 25 Apr 2012 15:06:28 +0200
branchdevel
changeset 277 f65edc0382cc
parent 258 0a5eb5aac0be
child 294 0e75bd39767d
permissions -rw-r--r--
prepare for release: * diffrent web directory with all web specific content (split doc dir -> doc (data) and web (rendered data)) * update installation

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)