Created
September 10, 2013 11:01
-
-
Save hpk42/6507878 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/structlog/threadlocal.py b/structlog/threadlocal.py | |
index 1b2eeb3..80de726 100644 | |
--- a/structlog/threadlocal.py | |
+++ b/structlog/threadlocal.py | |
@@ -17,11 +17,24 @@ Primitives to keep context global but thread local. | |
""" | |
import contextlib | |
-import threading | |
import uuid | |
from structlog._config import BoundLoggerLazyProxy | |
+try: | |
+ from greenlet import getcurrent | |
+except ImportError: | |
+ from threading import local as threadlocal | |
+else: | |
+ class threadlocal: | |
+ def __init__(self): | |
+ self.__dict__["_prefix"] = str(id(self)) | |
+ def __getattr__(self, name): | |
+ return getattr(getcurrent(), self._prefix + name) | |
+ def __setattr__(self, name, val): | |
+ setattr(getcurrent(), self._prefix + name, val) | |
+ def __delattr__(self, name): | |
+ delattr(getcurrent(), self._prefix + name) | |
def wrap_dict(dict_class): | |
""" | |
@@ -35,7 +48,7 @@ def wrap_dict(dict_class): | |
""" | |
Wrapped = type('WrappedDict-' + str(uuid.uuid4()), | |
(_ThreadLocalDictWrapper,), {}) | |
- Wrapped._tl = threading.local() | |
+ Wrapped._tl = threadlocal() | |
Wrapped._dict_class = dict_class | |
return Wrapped | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment