I was recently trying to deploy one app multiple times inside the same
WSGIDaemon ProcessGroup and WSGIApplicationGroup to reduce memory
usage.
This works quite well except for SQLAlchemy session which ends being
attached to the engine of the first app started.

Each WSGIScriptAlias, using the same WSGIProcessGroup, runs a
different app-instance.wsgi which creates a wsgi app from the same TG2
app using a different .ini file.

At first I got each app up and running with the correct configuration
options, but as soon as I make a request that accesses the DB each
virtual host starts giving the same results as the first one as they
all end up being connected to the engine of the first one.

I managed to solve the problem by replacing DBSession with something like this:

class DBSessionProxy(object):
    def __init__(self):
        self.session = None
        self.engines = {}

    def configure(self, *args, **kw):
        if not self.session:
            self.session = config['DBSession']
        self.session.configure(*args, **kw)

    def query(self, *args, **kw):
        if not self.session:
            self.session = config['DBSession']

        if not self.engines.has_key(config['sqlalchemy.url']):
            self.engines[config['sqlalchemy.url']] =
engine_from_config(config, 'sqlalchemy.')

        engine = self.engines[config['sqlalchemy.url']]
        self.session.configure(bind=engine)
        return self.session.query(*args, **kw)

Which rebinds the session before each and every single query.
I also tried to pass a specific scopefunc to scoped_session factory
which returns as the key the config['sqlalchemy.url'] +
str(thread.get_ident()) but that didn't change the behaviour (probably
because of all the session ending being configured to the same engine
anyway as init_model happens only one time and not at every single
request)

My solution works, but isn't really nice, does anyone know if there is
a cleaner way to solve the problem instead of creating my own
DBSession class?

-- 
You received this message because you are subscribed to the Google Groups 
"TurboGears" 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/turbogears?hl=en.

Reply via email to