Created
January 2, 2015 04:14
-
-
Save tiliv/ad9566f07e8e20ba52d7 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
# -*- coding: utf-8 -*- | |
from __future__ import unicode_literals | |
from __future__ import print_function | |
# ripped from djangotoolbox | |
# duplicated here to avoid external dependency | |
def make_tls_property(default=None): | |
"""Creates a class-wide instance property with a thread-specific value.""" | |
class TLSProperty(object): | |
def __init__(self): | |
from threading import local | |
self.local = local() | |
def __get__(self, instance, cls): | |
if not instance: | |
return self | |
return self.value | |
def __set__(self, instance, value): | |
self.value = value | |
def _get_value(self): | |
return getattr(self.local, 'value', default) | |
def _set_value(self, value): | |
self.local.value = value | |
value = property(_get_value, _set_value) | |
return TLSProperty() |
Author
tiliv
commented
Jan 2, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment