I am sorry abut the n00bish question. It really concerns other Python
WSGI frameworks in addition to Werkzeug.
Why simulate global variables with thread locals instead of binding
the data to the WSGI application instance and passing the application
to each view function?

Evidence 1 :http://code.google.com/p/modwsgi/wiki/
ProcessesAndThreading
'It is possible that a WSGI application could be executed at the same
time from multiple worker threads within the one child process. '
A WSGI app can be created once by a WSGI server, and then separate
threads will call the __call__ method to handle requests. Race
conditions danger!

Evidence 2
http://groups.google.com/group/pylons-discuss/browse_thread/thread/cfb7c394acc810ff
Still, if the application instance is the same, and the session is
bound to self, different threads should still see the same session, as
the Python GIL locks attribute access.\
 So there should be no threading problems.

Evidence 3
http://docs.repoze.org/bfg/1.2/narr/threadlocals.html#threadlocals-chapter
Repoze bfg developpers advocate never using thread locals (but they
still do use sqlalchemy scoped session)

This question has arised after I've begun an application which looks
like this:

class Application(object):
    def text(self, text_id):

        text = self.session.query(Submission).filter_by(id=text_id)
        resp = Response(mimetype='text/html')
        template = self.template_env.get_template('text.html')
        resp.data = template.render()
        return resp

    def __init__(self, url):
        self.template_env = Environment(loader=FileSystemLoader('/home/
ludovico/incoming/submissions'))
        self.session = initialize_models(url)

    def __call__(self, environ, start_response):
        adapter = url_map.bind_to_environ(environ)
        try:
            endpoint, values = adapter.match()
            response = getattr(self, endpoint)(values)
            return response(environ, start_response)
        except HTTPException, e:
            return e

def initialize_models(url):
    dbengine = create_engine(url, convert_unicode=True)
    Session = scoped_session(sessionmaker(bind=dbengine))
    return Sessionenviron, start_response)

In fact I wonder whether it's even necessary to use scoped_session at
this point.
I have never seen this done, so I wonder what the disadvantages are,
or if I am making a terrible mistake.

Regards,

Ludovico Fischer

-- 
You received this message because you are subscribed to the Google Groups 
"pocoo-libs" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/pocoo-libs?hl=en.

Reply via email to