Hello.

On 10.6.2013 18:41, Michael Bayer wrote:
> 
> On Jun 10, 2013, at 11:46 AM, Ladislav Lenart <lenart...@volny.cz> wrote:
> 
>>
>> I have no idea what is wrong. Please help me diagnose this! Note that OS 
>> monitor
>> shows several cherrypy threads, each gets its share, so the cherrypy setup 
>> seems
>> to be OK.
>> class _SessionContext(object):
>>    def __init__(self):
>>        self._level = 0
>>
>>    def __enter__(self):
>>        if self._level == 0:
>>            self._session = _Session()
>>        self._level += 1
>>        return self._session
>>
>>    def __exit__(self, type_, value, traceback):
>>        self._level -= 1
>>        if self._level == 0:
>>            self._session.close()
>>            self._session = None
>>
>> _thread_local = local()
>>
>> def get_session():
>>    ctx = getattr(_thread_local, 'session_context', None)
>>    if ctx:
>>        return ctx
>>    ctx = _SessionContext()
>>    _thread_local.session_context = ctx
>>    return ctx
> 
> 
> what's all this thread local stuff for?   The problem is likely there, a 
> scoped_session() already uses a threading.local.   A very simple usage 
> pattern should work fine.   Just put a "session.remove()" type of thing to 
> occur at the end of each request.   guidelines are here: 
> http://docs.sqlalchemy.org/en/rel_0_8/orm/session.html#using-thread-local-scope-with-web-applications
>  

Well, this is our way to call Session.remove() at the end of the request 
processing.

get_session() creates _SessionContext instance (a python context manager)
responsible for closing the session at the end. We use scoped_session() and we
wanted to support nesting like this:

    with get_session():
        with get_session():
            ...

Hence we create only one _SessionContext per thread. It increments its internal
counter whenever its __enter__() is called and decrements it whenever its
__exit__() is called. It closes the session when the counter reaches zero.

The decorator @session.use wraps the function in the call

    with get_session():

We mark every method of our handler that needs to access a database with this
decorator. It is always the top level decorator, i.e.:

    @session.use
    @other_decorator
    def foo():
        pass

I will investigate further and let you know.


Thank you,

Ladislav Lenart

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to