This is a proxy for an instance of some class called Foo. Any thread can use the one proxy, and the proxy will automatically look up the thread specific instance of Foo. This is great if you have a global function in someone else's code that you need to swap out for something "thread safe".
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | """Act as a proxy for the Foo instance in this thread's context."""
from ThreadLocalStorage import currentContext
class FooProxy:
"""Act as a proxy for the Foo instance in this thread's context.
This is based on the ThreadLocalStorage module.
"""
def __getattr__(self, attr):
return getattr(currentContext().foo, attr)
def __setattr__(self, attr, value):
setattr(currentContext().foo, attr, value)
|
Although this code uses "a non-standard" threading system, the basic idea is sound.
Tags: threads