Bundled cherrypy.
[smonitor.git] / monitor / cherrypy / _cpthreadinglocal.py
blob34c17ac41ad7bcbd8089d2c60da8170feffe8287
1 # This is a backport of Python-2.4's threading.local() implementation
3 """Thread-local objects
5 (Note that this module provides a Python version of thread
6 threading.local class. Depending on the version of Python you're
7 using, there may be a faster one available. You should always import
8 the local class from threading.)
10 Thread-local objects support the management of thread-local data.
11 If you have data that you want to be local to a thread, simply create
12 a thread-local object and use its attributes:
14 >>> mydata = local()
15 >>> mydata.number = 42
16 >>> mydata.number
19 You can also access the local-object's dictionary:
21 >>> mydata.__dict__
22 {'number': 42}
23 >>> mydata.__dict__.setdefault('widgets', [])
25 >>> mydata.widgets
28 What's important about thread-local objects is that their data are
29 local to a thread. If we access the data in a different thread:
31 >>> log = []
32 >>> def f():
33 ... items = mydata.__dict__.items()
34 ... items.sort()
35 ... log.append(items)
36 ... mydata.number = 11
37 ... log.append(mydata.number)
39 >>> import threading
40 >>> thread = threading.Thread(target=f)
41 >>> thread.start()
42 >>> thread.join()
43 >>> log
44 [[], 11]
46 we get different data. Furthermore, changes made in the other thread
47 don't affect data seen in this thread:
49 >>> mydata.number
52 Of course, values you get from a local object, including a __dict__
53 attribute, are for whatever thread was current at the time the
54 attribute was read. For that reason, you generally don't want to save
55 these values across threads, as they apply only to the thread they
56 came from.
58 You can create custom local objects by subclassing the local class:
60 >>> class MyLocal(local):
61 ... number = 2
62 ... initialized = False
63 ... def __init__(self, **kw):
64 ... if self.initialized:
65 ... raise SystemError('__init__ called too many times')
66 ... self.initialized = True
67 ... self.__dict__.update(kw)
68 ... def squared(self):
69 ... return self.number ** 2
71 This can be useful to support default values, methods and
72 initialization. Note that if you define an __init__ method, it will be
73 called each time the local object is used in a separate thread. This
74 is necessary to initialize each thread's dictionary.
76 Now if we create a local object:
78 >>> mydata = MyLocal(color='red')
80 Now we have a default number:
82 >>> mydata.number
85 an initial color:
87 >>> mydata.color
88 'red'
89 >>> del mydata.color
91 And a method that operates on the data:
93 >>> mydata.squared()
96 As before, we can access the data in a separate thread:
98 >>> log = []
99 >>> thread = threading.Thread(target=f)
100 >>> thread.start()
101 >>> thread.join()
102 >>> log
103 [[('color', 'red'), ('initialized', True)], 11]
105 without affecting this thread's data:
107 >>> mydata.number
109 >>> mydata.color
110 Traceback (most recent call last):
112 AttributeError: 'MyLocal' object has no attribute 'color'
114 Note that subclasses can define slots, but they are not thread
115 local. They are shared across threads:
117 >>> class MyLocal(local):
118 ... __slots__ = 'number'
120 >>> mydata = MyLocal()
121 >>> mydata.number = 42
122 >>> mydata.color = 'red'
124 So, the separate thread:
126 >>> thread = threading.Thread(target=f)
127 >>> thread.start()
128 >>> thread.join()
130 affects what we see:
132 >>> mydata.number
135 >>> del mydata
138 # Threading import is at end
140 class _localbase(object):
141 __slots__ = '_local__key', '_local__args', '_local__lock'
143 def __new__(cls, *args, **kw):
144 self = object.__new__(cls)
145 key = 'thread.local.' + str(id(self))
146 object.__setattr__(self, '_local__key', key)
147 object.__setattr__(self, '_local__args', (args, kw))
148 object.__setattr__(self, '_local__lock', RLock())
150 if args or kw and (cls.__init__ is object.__init__):
151 raise TypeError("Initialization arguments are not supported")
153 # We need to create the thread dict in anticipation of
154 # __init__ being called, to make sure we don't call it
155 # again ourselves.
156 dict = object.__getattribute__(self, '__dict__')
157 currentThread().__dict__[key] = dict
159 return self
161 def _patch(self):
162 key = object.__getattribute__(self, '_local__key')
163 d = currentThread().__dict__.get(key)
164 if d is None:
165 d = {}
166 currentThread().__dict__[key] = d
167 object.__setattr__(self, '__dict__', d)
169 # we have a new instance dict, so call out __init__ if we have
170 # one
171 cls = type(self)
172 if cls.__init__ is not object.__init__:
173 args, kw = object.__getattribute__(self, '_local__args')
174 cls.__init__(self, *args, **kw)
175 else:
176 object.__setattr__(self, '__dict__', d)
178 class local(_localbase):
180 def __getattribute__(self, name):
181 lock = object.__getattribute__(self, '_local__lock')
182 lock.acquire()
183 try:
184 _patch(self)
185 return object.__getattribute__(self, name)
186 finally:
187 lock.release()
189 def __setattr__(self, name, value):
190 lock = object.__getattribute__(self, '_local__lock')
191 lock.acquire()
192 try:
193 _patch(self)
194 return object.__setattr__(self, name, value)
195 finally:
196 lock.release()
198 def __delattr__(self, name):
199 lock = object.__getattribute__(self, '_local__lock')
200 lock.acquire()
201 try:
202 _patch(self)
203 return object.__delattr__(self, name)
204 finally:
205 lock.release()
208 def __del__():
209 threading_enumerate = enumerate
210 __getattribute__ = object.__getattribute__
212 def __del__(self):
213 key = __getattribute__(self, '_local__key')
215 try:
216 threads = list(threading_enumerate())
217 except:
218 # if enumerate fails, as it seems to do during
219 # shutdown, we'll skip cleanup under the assumption
220 # that there is nothing to clean up
221 return
223 for thread in threads:
224 try:
225 __dict__ = thread.__dict__
226 except AttributeError:
227 # Thread is dying, rest in peace
228 continue
230 if key in __dict__:
231 try:
232 del __dict__[key]
233 except KeyError:
234 pass # didn't have anything in this thread
236 return __del__
237 __del__ = __del__()
239 from threading import currentThread, enumerate, RLock