On Wed, Dec 16, 2015 at 4:32 PM, Mike Bayer <mike...@zzzcomputing.com>
wrote:

> > This works ok, and it's leaking less memory than it was before.
>
> OK well it's not like we're trying to seal with duct tape, any leak
> means, "it's broke" :)


Well, I expect we're responsible for causing some, if not most, of these
memory leaks.

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.
>
> You can make event-independent "proxies" of engines using the
> engine.execution_options() feature.   You can define whatever keywords
> you want there (or not send any), and you can make event listeners that
> are local to that engine and alternatively can peek into the
> _execution_options dictionary (which means you could really just have
> one listener that dispatches based on something in the dictionary).
>

Awesome, this is exactly what I needed.  It does leak a little bit more
memory (about 1400k after 1000 iterations) than the copy() method, but I
won't be using that.


> from sqlalchemy import create_engine, event
>
> e = create_engine("sqlite://", echo=True)
>
>
> def listen_for_thing(keyword, engine):
>     @event.listens_for(engine, "before_cursor_execute")
>     def before_cursor_execute(
>             conn, cursor, statement, parameters, context, executemany):
>         print("statement for engine %s / %s: '%s'" % (
>             keyword, conn._execution_options['our_engine_name'],
> statement))
>
>
> e1 = e.execution_options(our_engine_name='engine1')
> e2 = e.execution_options(our_engine_name='engine2')
>
> listen_for_thing('engine1', e1)
> listen_for_thing('engine2', e2)
>
> e1.execute("select 'engine1'")
> e2.execute("select 'engine2'")
>


Thanks for the incredibly fast response,
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