您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

54 行
1.7KB

  1. import weakref
  2. from eventlet import greenthread
  3. __all__ = ['get_ident', 'local']
  4. def get_ident():
  5. """ Returns ``id()`` of current greenlet. Useful for debugging."""
  6. return id(greenthread.getcurrent())
  7. # the entire purpose of this class is to store off the constructor
  8. # arguments in a local variable without calling __init__ directly
  9. class _localbase(object):
  10. __slots__ = '_local__args', '_local__greens'
  11. def __new__(cls, *args, **kw):
  12. self = object.__new__(cls)
  13. object.__setattr__(self, '_local__args', (args, kw))
  14. object.__setattr__(self, '_local__greens', weakref.WeakKeyDictionary())
  15. if (args or kw) and (cls.__init__ is object.__init__):
  16. raise TypeError("Initialization arguments are not supported")
  17. return self
  18. def _patch(thrl):
  19. greens = object.__getattribute__(thrl, '_local__greens')
  20. # until we can store the localdict on greenlets themselves,
  21. # we store it in _local__greens on the local object
  22. cur = greenthread.getcurrent()
  23. if cur not in greens:
  24. # must be the first time we've seen this greenlet, call __init__
  25. greens[cur] = {}
  26. cls = type(thrl)
  27. if cls.__init__ is not object.__init__:
  28. args, kw = object.__getattribute__(thrl, '_local__args')
  29. thrl.__init__(*args, **kw)
  30. object.__setattr__(thrl, '__dict__', greens[cur])
  31. class local(_localbase):
  32. def __getattribute__(self, attr):
  33. _patch(self)
  34. return object.__getattribute__(self, attr)
  35. def __setattr__(self, attr, value):
  36. _patch(self)
  37. return object.__setattr__(self, attr, value)
  38. def __delattr__(self, attr):
  39. _patch(self)
  40. return object.__delattr__(self, attr)