get_current_greenlet = greenlet.getcurrent del greenlet except: get_current_greenlet = int try: from thread import get_ident as get_current_thread, allocate_lock except ImportError: from dummy_thread import get_ident as get_current_thread, allocate_lock from werkzeug.wsgi import ClosingIterator from werkzeug._internal import _patch_wrapper if get_current_greenlet is int: get_ident = get_current_thread else: get_ident = lambda: (get_current_thread(), get_current_greenlet()) def release_local(local): local.__release_local__() class Local(object): __slots__ = ('__storage__', '__lock__') def __init__(self): object.__setattr__(self, '__storage__', {}) object.__setattr__(self, '__lock__', allocate_lock()) def __iter__(self): return self.__storage__.iteritems()
def get_ident(): return get_current_thread(), get_current_greenlet()
# coding: utf-8 try: from greenlet import greenlet get_current_greenlet = greenlet.getcurrent del greenlet except: get_current_greenlet = int from thread import get_ident as get_current_thread, allocate_lock if get_current_greenlet is int: get_ident = get_current_thread else: get_ident = lambda: (get_current_thread(), get_current_thread()) class Local(object): """ 类似ThreadLocal的实现, 可以应付协程场景。 """ __slots__ = ('__storage__', '__lock__') def __init__(self): object.__setattr__(self, '__storage__', {}) object.__setattr__(self, '__lock__', allocate_lock()) def __iter__(self): return self.__storage__.iteritems()
get_current_greenlet = int try: from thread import get_ident as get_current_thread, allocate_lock except ImportError: from dummy_thread import get_ident as get_current_thread, allocate_lock from werkzeug.utils import ClosingIterator from werkzeug._internal import _patch_wrapper # get the best ident function. if greenlets are not installed we can # savely just use the builtin thread function and save a python methodcall # and the cost of caculating a hash. if get_current_greenlet is int: get_ident = get_current_thread else: get_ident = lambda: hash((get_current_thread(), get_current_greenlet())) class Local(object): __slots__ = ('__storage__', '__lock__') def __init__(self): object.__setattr__(self, '__storage__', {}) object.__setattr__(self, '__lock__', allocate_lock()) def __iter__(self): return self.__storage__.iteritems() def __call__(self, proxy): """Create a proxy for a name.""" return LocalProxy(self, proxy)
def get_ident(): """ Return a unique number for the current greenlet in the current thread. """ return hash((get_current_thread(), get_current_greenlet()))