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()
 

Reply via email to