Skip to content

Instantly share code, notes, and snippets.

@tiliv
Created January 2, 2015 04:14
Show Gist options
  • Save tiliv/ad9566f07e8e20ba52d7 to your computer and use it in GitHub Desktop.
Save tiliv/ad9566f07e8e20ba52d7 to your computer and use it in GitHub Desktop.
# -*- 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()
@tiliv
Copy link
Author

tiliv commented Jan 2, 2015

from django.conf import settings
from .utils import make_tls_property

SITE_ID = settings.__dict__['_wrapped'].__class__.SITE_ID = make_tls_property(settings.SITE_ID)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment