diff options
author | Jason Kirtland <jek@discorporate.us> | 2007-08-15 23:11:47 +0000 |
---|---|---|
committer | Jason Kirtland <jek@discorporate.us> | 2007-08-15 23:11:47 +0000 |
commit | 46535b25a642b596c1f43217fe00f6205bcc0ecb (patch) | |
tree | dc721e69f2d0b1f4a97b2f45f4071b1a3d63c831 /lib/sqlalchemy/util.py | |
parent | fddc687348fe5e5b36b3207e5ff454bb901b64bc (diff) | |
download | sqlalchemy-46535b25a642b596c1f43217fe00f6205bcc0ecb.tar.gz |
use threading.local if available
speed up ThreadLocal for python 2.3 [ticket:743]
clean in topo (in patch from [ticket:743])
Diffstat (limited to 'lib/sqlalchemy/util.py')
-rw-r--r-- | lib/sqlalchemy/util.py | 46 |
1 files changed, 26 insertions, 20 deletions
diff --git a/lib/sqlalchemy/util.py b/lib/sqlalchemy/util.py index 82815f101..37dfeb211 100644 --- a/lib/sqlalchemy/util.py +++ b/lib/sqlalchemy/util.py @@ -366,26 +366,32 @@ class OrderedDict(dict): self._list.remove(item[0]) return item -class ThreadLocal(object): - """An object in which attribute access occurs only within the context of the current thread.""" - - def __init__(self): - self.__dict__['_tdict'] = {} - - def __delattr__(self, key): - try: - del self._tdict["%d_%s" % (thread.get_ident(), key)] - except KeyError: - raise AttributeError(key) - - def __getattr__(self, key): - try: - return self._tdict["%d_%s" % (thread.get_ident(), key)] - except KeyError: - raise AttributeError(key) - - def __setattr__(self, key, value): - self._tdict["%d_%s" % (thread.get_ident(), key)] = value +try: + from threading import local as ThreadLocal +except ImportError: + try: + from dummy_threading import local as ThreadLocal + except ImportError: + class ThreadLocal(object): + """An object in which attribute access occurs only within the context of the current thread.""" + + def __init__(self): + self.__dict__['_tdict'] = {} + + def __delattr__(self, key): + try: + del self._tdict[(thread.get_ident(), key)] + except KeyError: + raise AttributeError(key) + + def __getattr__(self, key): + try: + return self._tdict[(thread.get_ident(), key)] + except KeyError: + raise AttributeError(key) + + def __setattr__(self, key, value): + self._tdict[(thread.get_ident(), key)] = value class DictDecorator(dict): """A Dictionary that delegates items not found to a second wrapped dictionary.""" |