[sqlalchemy] Re: Modifying the scopefunc of an existing ScopedSession
Gaetan de Menten wrote: Since I was curious about the reason this didn't work, I looked more closely at that part of the code and I don't like that __new__ trick: it doesn't really help simplify the code and can be surprising. Attached patch suppress it. thank you, this is committed in r6420 r6421 0.5/0.6 --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: Modifying the scopefunc of an existing ScopedSession
Hi Gaetan, On Oct 14, 5:11 pm, Gaetan de Menten gdemen...@gmail.com wrote: On Wed, Oct 14, 2009 at 17:09, Gaetan de Menten gdemen...@gmail.com wrote: Btw: Iwan, did you try: factory = elixir.session.session_factory elixir.session.registry = sqlalchemy.util.ScopedRegistry(factory, scope_func=your_scope_func) I gave up wanting to fiddle with what felt too much like sqlalchemy internals before I saw this post of yours. Since you went to the trouble though, I went back and tried your code - and it seems to work. (BTW, scope_func should just be spelled scopefunc.) Thanks, will have to sleep on which route to go now - but I'm leaning towards doing this instead of having __session__ all over the show. -i --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: Modifying the scopefunc of an existing ScopedSession
On Tue, Oct 13, 2009 at 16:49, Michael Bayer mike...@zzzcomputing.com wrote: We have a situation where we have an existing ScopedSession, but want to change its scopefunc. This sounds like a strange requirement, it is because we use elixir - the issue is discusses here: http://groups.google.com/group/sqlelixir/browse_thread/thread/623f190c1784e5e9 How could we do this? To test, we currently we do: elixir.session.registry.scopefunc = lambda: 1 (The default is thread-local) But we seem to still end up with different sessions in different threads. you have to set that up ahead of time. by default, the registry evaluates as a _TLocalRegistry which is hardcoded to threadlocal. Session = scoped_session(sessionmaker(), scopefunc=lambda: 1) Since I was curious about the reason this didn't work, I looked more closely at that part of the code and I don't like that __new__ trick: it doesn't really help simplify the code and can be surprising. Attached patch suppress it. Btw: Iwan, did you try: factory = elixir.session.session_factory elixir.session.registry = sqlalchemy.util.ScopedRegistry(maker, scope_func=your_scope_func) Kinda ugly, but should work... (hopefully) -- Gaëtan de Menten http://openhex.org --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~--- Index: orm/scoping.py === --- orm/scoping.py (revision 6393) +++ orm/scoping.py (working copy) @@ -5,7 +5,8 @@ # the MIT License: http://www.opensource.org/licenses/mit-license.php import sqlalchemy.exceptions as sa_exc -from sqlalchemy.util import ScopedRegistry, to_list, get_cls_kwargs, deprecated +from sqlalchemy.util import ScopedRegistry, ThreadLocalRegistry, \ +to_list, get_cls_kwargs, deprecated from sqlalchemy.orm import ( EXT_CONTINUE, MapperExtension, class_mapper, object_session ) @@ -29,7 +30,10 @@ def __init__(self, session_factory, scopefunc=None): self.session_factory = session_factory -self.registry = ScopedRegistry(session_factory, scopefunc) +if scopefunc: +self.registry = ScopedRegistry(session_factory, scopefunc) +else: +self.registry = ThreadLocalRegistry(session_factory) self.extension = _ScopedExt(self) def __call__(self, **kwargs): Index: util.py === --- util.py (revision 6393) +++ util.py (working copy) @@ -1163,14 +1163,7 @@ scopefunc a callable that will return a key to store/retrieve an object. - If None, ScopedRegistry uses a threading.local object instead. - -def __new__(cls, createfunc, scopefunc=None): -if not scopefunc: -return object.__new__(_TLocalRegistry) -else: -return object.__new__(cls) def __init__(self, createfunc, scopefunc): self.createfunc = createfunc @@ -1196,8 +1189,8 @@ except KeyError: pass -class _TLocalRegistry(ScopedRegistry): -def __init__(self, createfunc, scopefunc=None): +class ThreadLocalRegistry(ScopedRegistry): +def __init__(self, createfunc): self.createfunc = createfunc self.registry = threading.local()
[sqlalchemy] Re: Modifying the scopefunc of an existing ScopedSession
On Wed, Oct 14, 2009 at 17:09, Gaetan de Menten gdemen...@gmail.com wrote: Btw: Iwan, did you try: factory = elixir.session.session_factory elixir.session.registry = sqlalchemy.util.ScopedRegistry(maker, scope_func=your_scope_func) Of course, that should read : factory = elixir.session.session_factory elixir.session.registry = sqlalchemy.util.ScopedRegistry(factory, scope_func=your_scope_func) -- Gaëtan de Menten http://openhex.org --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: Modifying the scopefunc of an existing ScopedSession
Iwan wrote: Hi there, We have a situation where we have an existing ScopedSession, but want to change its scopefunc. This sounds like a strange requirement, it is because we use elixir - the issue is discusses here: http://groups.google.com/group/sqlelixir/browse_thread/thread/623f190c1784e5e9 How could we do this? To test, we currently we do: elixir.session.registry.scopefunc = lambda: 1 (The default is thread-local) But we seem to still end up with different sessions in different threads. you have to set that up ahead of time. by default, the registry evaluates as a _TLocalRegistry which is hardcoded to threadlocal. Session = scoped_session(sessionmaker(), scopefunc=lambda: 1) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---