_threading_local.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. """Thread-local objects.
  2. (Note that this module provides a Python version of the threading.local
  3. class. Depending on the version of Python you're using, there may be a
  4. faster one available. You should always import the `local` class from
  5. `threading`.)
  6. Thread-local objects support the management of thread-local data.
  7. If you have data that you want to be local to a thread, simply create
  8. a thread-local object and use its attributes:
  9. >>> mydata = local()
  10. >>> mydata.number = 42
  11. >>> mydata.number
  12. 42
  13. You can also access the local-object's dictionary:
  14. >>> mydata.__dict__
  15. {'number': 42}
  16. >>> mydata.__dict__.setdefault('widgets', [])
  17. []
  18. >>> mydata.widgets
  19. []
  20. What's important about thread-local objects is that their data are
  21. local to a thread. If we access the data in a different thread:
  22. >>> log = []
  23. >>> def f():
  24. ... items = mydata.__dict__.items()
  25. ... items.sort()
  26. ... log.append(items)
  27. ... mydata.number = 11
  28. ... log.append(mydata.number)
  29. >>> import threading
  30. >>> thread = threading.Thread(target=f)
  31. >>> thread.start()
  32. >>> thread.join()
  33. >>> log
  34. [[], 11]
  35. we get different data. Furthermore, changes made in the other thread
  36. don't affect data seen in this thread:
  37. >>> mydata.number
  38. 42
  39. Of course, values you get from a local object, including a __dict__
  40. attribute, are for whatever thread was current at the time the
  41. attribute was read. For that reason, you generally don't want to save
  42. these values across threads, as they apply only to the thread they
  43. came from.
  44. You can create custom local objects by subclassing the local class:
  45. >>> class MyLocal(local):
  46. ... number = 2
  47. ... initialized = False
  48. ... def __init__(self, **kw):
  49. ... if self.initialized:
  50. ... raise SystemError('__init__ called too many times')
  51. ... self.initialized = True
  52. ... self.__dict__.update(kw)
  53. ... def squared(self):
  54. ... return self.number ** 2
  55. This can be useful to support default values, methods and
  56. initialization. Note that if you define an __init__ method, it will be
  57. called each time the local object is used in a separate thread. This
  58. is necessary to initialize each thread's dictionary.
  59. Now if we create a local object:
  60. >>> mydata = MyLocal(color='red')
  61. Now we have a default number:
  62. >>> mydata.number
  63. 2
  64. an initial color:
  65. >>> mydata.color
  66. 'red'
  67. >>> del mydata.color
  68. And a method that operates on the data:
  69. >>> mydata.squared()
  70. 4
  71. As before, we can access the data in a separate thread:
  72. >>> log = []
  73. >>> thread = threading.Thread(target=f)
  74. >>> thread.start()
  75. >>> thread.join()
  76. >>> log
  77. [[('color', 'red'), ('initialized', True)], 11]
  78. without affecting this thread's data:
  79. >>> mydata.number
  80. 2
  81. >>> mydata.color
  82. Traceback (most recent call last):
  83. ...
  84. AttributeError: 'MyLocal' object has no attribute 'color'
  85. Note that subclasses can define slots, but they are not thread
  86. local. They are shared across threads:
  87. >>> class MyLocal(local):
  88. ... __slots__ = 'number'
  89. >>> mydata = MyLocal()
  90. >>> mydata.number = 42
  91. >>> mydata.color = 'red'
  92. So, the separate thread:
  93. >>> thread = threading.Thread(target=f)
  94. >>> thread.start()
  95. >>> thread.join()
  96. affects what we see:
  97. >>> mydata.number
  98. 11
  99. >>> del mydata
  100. """
  101. __all__ = ["local"]
  102. # We need to use objects from the threading module, but the threading
  103. # module may also want to use our `local` class, if support for locals
  104. # isn't compiled in to the `thread` module. This creates potential problems
  105. # with circular imports. For that reason, we don't import `threading`
  106. # until the bottom of this file (a hack sufficient to worm around the
  107. # potential problems). Note that almost all platforms do have support for
  108. # locals in the `thread` module, and there is no circular import problem
  109. # then, so problems introduced by fiddling the order of imports here won't
  110. # manifest on most boxes.
  111. class _localbase(object):
  112. __slots__ = '_local__key', '_local__args', '_local__lock'
  113. def __new__(cls, *args, **kw):
  114. self = object.__new__(cls)
  115. key = '_local__key', 'thread.local.' + str(id(self))
  116. object.__setattr__(self, '_local__key', key)
  117. object.__setattr__(self, '_local__args', (args, kw))
  118. object.__setattr__(self, '_local__lock', RLock())
  119. if (args or kw) and (cls.__init__ is object.__init__):
  120. raise TypeError("Initialization arguments are not supported")
  121. # We need to create the thread dict in anticipation of
  122. # __init__ being called, to make sure we don't call it
  123. # again ourselves.
  124. dict = object.__getattribute__(self, '__dict__')
  125. current_thread().__dict__[key] = dict
  126. return self
  127. def _patch(self):
  128. key = object.__getattribute__(self, '_local__key')
  129. d = current_thread().__dict__.get(key)
  130. if d is None:
  131. d = {}
  132. current_thread().__dict__[key] = d
  133. object.__setattr__(self, '__dict__', d)
  134. # we have a new instance dict, so call out __init__ if we have
  135. # one
  136. cls = type(self)
  137. if cls.__init__ is not object.__init__:
  138. args, kw = object.__getattribute__(self, '_local__args')
  139. cls.__init__(self, *args, **kw)
  140. else:
  141. object.__setattr__(self, '__dict__', d)
  142. class local(_localbase):
  143. def __getattribute__(self, name):
  144. lock = object.__getattribute__(self, '_local__lock')
  145. lock.acquire()
  146. try:
  147. _patch(self)
  148. return object.__getattribute__(self, name)
  149. finally:
  150. lock.release()
  151. def __setattr__(self, name, value):
  152. if name == '__dict__':
  153. raise AttributeError(
  154. "%r object attribute '__dict__' is read-only"
  155. % self.__class__.__name__)
  156. lock = object.__getattribute__(self, '_local__lock')
  157. lock.acquire()
  158. try:
  159. _patch(self)
  160. return object.__setattr__(self, name, value)
  161. finally:
  162. lock.release()
  163. def __delattr__(self, name):
  164. if name == '__dict__':
  165. raise AttributeError(
  166. "%r object attribute '__dict__' is read-only"
  167. % self.__class__.__name__)
  168. lock = object.__getattribute__(self, '_local__lock')
  169. lock.acquire()
  170. try:
  171. _patch(self)
  172. return object.__delattr__(self, name)
  173. finally:
  174. lock.release()
  175. def __del__(self):
  176. import threading
  177. key = object.__getattribute__(self, '_local__key')
  178. try:
  179. # We use the non-locking API since we might already hold the lock
  180. # (__del__ can be called at any point by the cyclic GC).
  181. threads = threading._enumerate()
  182. except:
  183. # If enumerating the current threads fails, as it seems to do
  184. # during shutdown, we'll skip cleanup under the assumption
  185. # that there is nothing to clean up.
  186. return
  187. for thread in threads:
  188. try:
  189. __dict__ = thread.__dict__
  190. except AttributeError:
  191. # Thread is dying, rest in peace.
  192. continue
  193. if key in __dict__:
  194. try:
  195. del __dict__[key]
  196. except KeyError:
  197. pass # didn't have anything in this thread
  198. from threading import current_thread, RLock