Hi there, I have an idea I'd like to bounce off people here. It relates to the problem described here: http://www.sqlalchemy.org/docs/05/session.html#unitofwork_contextual
The solution used there makes the assumption that each request will happen in its own thread, and uses python's thread-local stuff to do its magic. I was wondering if it is not possible to do something that is local to the current call-stack, instead of the current thread. I think there are two issues here: a) a general mechanism for remembering a call-local context b) using it to provide a call-local sqlalchemy session Now, I'm not sure yet how to do (b), but want to give an example of (a) with a naive implementation to illustrate the idea. (Just note this mechanism can be used for other important contextual info as well, such as who the current user is, etc) With the following code, I can call a method b.bar, passing it some "context" (which is not part of the method's original signature). b.bar calls a.foo in turn, also not passing it anything, but inside A.foo, the context can be retrieved from the call stack. The only overhead incurred is during this last step, and I don't think it is much. (Implementation of CC is at the bottom.) Any thoughts? class A(object): def foo(self): print CC.get_context() #CC here is assumed to be a class, on which a class method is # called to get the current "context", which may be our session # but I think it can be used for more things than just the session class B(object): a = A() def bar(self, arg): print arg self.a.foo() b = B() CC(b.bar, 'context stuff - a session or a dict?', 'some argument for bar()') #--------------------- # CC (should be before the examples above if you want to run it) # import inspect class CC(object): def __init__(self, callable, context, *args, **kwargs): self.callable = callable self.context = context self.callable(*args, **kwargs) @classmethod def get_context(cls): context = None found = False f = inspect.currentframe() while not found and f.f_back: theSelf = f.f_locals.get('self', None) if theSelf.__class__ is cls: context = theSelf.context found = True nextF = f.f_back del f f = nextF return context --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~----------~----~----~----~------~----~------~--~---