Skip to content

Instantly share code, notes, and snippets.

@hpk42
Created September 10, 2013 10:49
Show Gist options
  • Save hpk42/6507777 to your computer and use it in GitHub Desktop.
Save hpk42/6507777 to your computer and use it in GitHub Desktop.
diff --git a/structlog/threadlocal.py b/structlog/threadlocal.py
index 1b2eeb3..05f40ea 100644
--- a/structlog/threadlocal.py
+++ b/structlog/threadlocal.py
@@ -22,6 +22,34 @@ import uuid
from structlog._config import BoundLoggerLazyProxy
+try:
+ from greenlet import getcurrent as _getident
+except ImportError:
+ try:
+ from thread import get_ident as _getident
+ except ImportError:
+ try:
+ from _thread import get_ident as _getident
+ except ImportError:
+ from dummy_thread import get_ident as _getident
+
+class dictlocal:
+ def __init__(self):
+ self._storage = {}
+
+ def __getitem__(self, key):
+ return self._storage[_getident()][key]
+
+ def __setitem__(self, key, value):
+ try:
+ d = self._storage[_getident()]
+ except KeyError:
+ d = self._storage[_getident()] = {}
+ d[key] = value
+
+ def __delitem__(self, key):
+ del self._storage[_getident()][key]
+
def wrap_dict(dict_class):
"""
@@ -35,7 +63,7 @@ def wrap_dict(dict_class):
"""
Wrapped = type('WrappedDict-' + str(uuid.uuid4()),
(_ThreadLocalDictWrapper,), {})
- Wrapped._tl = threading.local()
+ Wrapped._tl = dictlocal()
Wrapped._dict_class = dict_class
return Wrapped
@@ -53,7 +81,7 @@ def as_immutable(logger):
logger = logger.bind()
try:
- ctx = logger._context._tl.dict_.__class__(logger._context._dict)
+ ctx = logger._context._tl["dict_"].__class__(logger._context._dict)
bl = logger.__class__(
logger._logger,
processors=logger._processors,
@@ -114,10 +142,10 @@ class _ThreadLocalDictWrapper(object):
Return or create and return the current context.
"""
try:
- return self.__class__._tl.dict_
- except AttributeError:
- self.__class__._tl.dict_ = self.__class__._dict_class()
- return self.__class__._tl.dict_
+ return self.__class__._tl["dict_"]
+ except KeyError:
+ self.__class__._tl["dict_"] = x = self.__class__._dict_class()
+ return x
def __repr__(self):
return '<{0}({1!r})>'.format(self.__class__.__name__, self._dict)
@hpk42
Copy link
Author

hpk42 commented Sep 10, 2013

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