Skip to content

Instantly share code, notes, and snippets.

@hpk42
Created September 10, 2013 11:01
Show Gist options
  • Save hpk42/6507878 to your computer and use it in GitHub Desktop.
Save hpk42/6507878 to your computer and use it in GitHub Desktop.
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