Hi,

I work on a project that uses dependency injection.  We want to log queries
in the context of a particular instantiation of the injector.  This means
that when we register an event listener, that listener has references to a
unique logger configuration and we don't want that listener to leak into
other contexts.

I think the way to do this is to use a separate Engine for each injector,
but I still want to have a shared connection pool.  My first attempt looked
similar to this:

    _pool = None
    @Injector.factory('db_engine')
    def engine_factory():
        nonlocal _pool
        engine = create_engine(dburi, pool=_pool)
        if _pool is None:
            _pool = engine.pool
        return engine

This worked.  Some of the engine events are actually pool events, so those
couldn't be isolated, but I was able to avoid using those.  This also leaks
memory (I think it's related to the new dialect object each time we create
an engine).  Perhaps this isn't how connection pools were meant to be used.

Now I'm considering another way to isolate the engine and it's event
listeners.  It looks like this:

    _engine = None
    @Injector.factory('db_engine')
    def engine_factory():
        nonlocal _engine
        if _engine is None:
            _engine = create_engine(dburi)
        engine = copy.copy(_engine)
        engine.dispatch = copy.copy(engine.dispatch)
        return engine

This works ok, and it's leaking less memory than it was before.  But, I've
made some assumptions that I probably shouldn't be making.

So, I'm looking for comments or advice on whether this is a terrible idea,
or if there's another way I should be approaching this.


Thanks,
Kai

-- 
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 https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to