-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread_local.py
More file actions
425 lines (341 loc) · 13.3 KB
/
Copy paththread_local.py
File metadata and controls
425 lines (341 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
"""
This module inspired by Flask, threading.local and werkzeug.local implementation
See code:
https://github.com/python/cpython/blob/master/Lib/_threading_local.py
http://werkzeug.pocoo.org/docs/0.12/local/
The module provides local context object with manager and proxy helpers.
Local context object is a special object which defines globally providing
ability to thread-safe access and manipulation with it attributes.
CRUD operation set guarantied :)
E.g. you can use one generally global object with attribute values would be local
and separated to each thread without any interference.
Simple and thou useless example in main thread:
>>> manager = BaseContextManager()
>>> local = LocalProxy(manager)
>>> data = local()
>>> data.results = 99
>>> print(data.results)
99
>>> data.results = 0
>>> print(data.results)
0
>>> del data.results
>>> print(data.results)
None
Still nothing to see here but if we dive deep into threads..:
>>> from concurrent import futures
Lets define attribute global variable:
>>> def func(n):
... results = n
... return results
...
>>> data = local()
>>> results = data.results = 99
And run function 'func' in ten separate threads:
>>> fs = []
>>> with futures.ThreadPoolExecutor(10) as executor:
... for i in range(10):
...... fs.append(executor.submit(func, i))
... for future in futures.as_completed(fs):
...... print(future.result())
1
0
3
4
2
5
6
7
8
9
Print main thread result variable value:
>>> print(results)
99
As we can see the value of variable results is independent between each thread.
Any changes made to it value in any context (thread context) are not affecting
each other.
Furthermore, you can access __dict__ attribute of local context object and get
only context-defined attributes:
>>> print(data.__dict__())
{'results': 99}
Note that __dict__ is method, not property/class attribute and thou it need to be called!
Also, local context object support attribute delete operation. Just as simple
as expected:
>>> del data.results
>>> print(data.results)
None
Another use-case is to implement own ContextLocal and LocalProxy class for
example to add default arguments for local context objects:
>>> class ModLocal(ContextLocal):
...
... def __init__(self, manager, **kwargs):
...... {manager.set(self, k, v) for k,v in kwargs.items()}
>>> class ModProxy(LocalProxy):
... def __init__(self, manager):
...... self.manage = manager
...
... def __call__(self, **kwargs):
...... return Modified(manager, **kwargs)
>>> manager = BaseContextManager()
>>> local = ModProxy(manager)
>>> data = local(color='red')
>>> print(data.color)
red
BaseContextManager could be subclassed too. Use-cases are:
- implementing read-only variables aka constants
- redesigning descriptors logic
- improve thread control functionality
- etc
"""
from threading import RLock, get_ident
class ContextLocal(object):
"""
ContextLocal class implements thread-safe local context object.
Each time you add attribute to such object - it's bound to
thread within the context of which this variable was assigned.
"""
def __new__(cls, manager: object, *args, **kwargs):
"""
This method run before class instantiated to bind each new instance
with context manager and store special meta attributes.
:param manager: BaseContextManager
:param args: object, *
:param kwargs: **
:return: object
"""
# case: since Py 2.5 object __init__ method accept no arguments
# this check should prevent creating instance with init arguments
# but without own __init__ method implementation
if (args or kwargs) and (cls.__init__ is object.__init__):
raise TypeError("Initialization arguments are not supported")
self = super().__new__(cls)
id = manager.ident_func()
name = cls.__name__
object.__setattr__(self, '_{}__manager'.format(name), manager)
object.__setattr__(self, '_{}__largs'.format(name), (args, kwargs))
object.__setattr__(self, '_{}__lock'.format(name), RLock())
object.__setattr__(self, '__name__', name)
attrs_dict = object.__getattribute__(self, '__dict__')
manager.register(attrs_dict, id)
return self
def __getattribute__(self, name):
cls_name = object.__getattribute__(self, '__name__')
lock = object.__getattribute__(self, '_{}__lock'.format(cls_name))
with lock:
manager = object.__getattribute__(self, '_{}__manager'.format(cls_name))
return manager.get(name)
def __setattr__(self, name, value):
cls_name = object.__getattribute__(self, '__name__')
lock = object.__getattribute__(self, '_{}__lock'.format(cls_name))
with lock:
manager = object.__getattribute__(self, '_{}__manager'.format(cls_name))
manager.set(self, name, value)
def __delattr__(self, name):
cls_name = object.__getattribute__(self, '__name__')
lock = object.__getattribute__(self, '_{}__lock'.format(cls_name))
with lock:
manager = object.__getattribute__(self, '_{}__manager'.format(cls_name))
manager.delete(name)
def __del__(self):
cls_name = object.__getattribute__(self, '__name__')
lock = object.__getattribute__(self, '_{}__lock'.format(cls_name))
with lock:
manager = object.__getattribute__(self, '_{}__manager'.format(cls_name))
if self in manager:
manager.unregister()
class LocalProxy():
"""
This class implements simple broker between current context
and LocalContext object
"""
def __init__(self, manager):
"""
Manager argument will be used to instantiate ContextLocal
class.
:param manager:
"""
self.manager = manager
def __call__(self, *args, **kwargs):
"""
Create ContextLocal object bound to specific manager.
args/kwargs left for compatibility reasons. They haven't
been used in basic ContextManager class.
:param args: *
:param kwargs: **
:return: ContextLocal
"""
if self.manager:
return ContextLocal(self.manager, *args, **kwargs)
raise AttributeError('No manager provided to manage local object')
class BaseContextManager(object):
"""
This class is used to manage operations with access descriptors
of local context objects.
BaseContextManager perform separate and granular access to bunch
of <thread: local_object>.
E.g it proxies calls to appropriate objects from different contexts(threads).
"""
SPECIAL_METHODS = ['manager', 'largs', 'lock']
def __init__(self, ident_fn=None):
"""
Initialize ContextManager with identification function. It used
to identify thread which want to get/set/del attributes of local
context object.
Leave None to use threading get_ident() implementation.
:param ident_fn: function or None
"""
self.objects = {}
self.ident_func = ident_fn if ident_fn else self._get_ident()
self.lock = RLock()
def __contains__(self, item):
"""
Check if thread with ID(item) is managed by ContextManager.
:param item: int
:return: bool
"""
# TODO: replace specific class check with abstract class check
if isinstance(item, (ContextLocal,)):
item = self.ident_func()
return item in self.objects.keys()
def register(self, attrs: dict, id=None):
"""
This is the only method allowed to add(register) threads
to store local object context in ContextManager objects dictionary.
One's thread is registered it can store/access/remove local object
context.
Consider that threads could be registered not only by direct use
of this method, but also just by assigning new attribute value to local
context object - set() method will register thread automatically by
invoking register() method.
:param attrs: dict
:param id: int or None
:return: None
"""
id = id if id else self.ident_func()
attrs['__dict__'] = self._get_dict
with self.lock:
self.objects[id] = attrs
def unregister(self, id=None):
"""
This method invokes before local context object is deleted
by garbage collector e.g when thread-owner of this object
was deleted.
:param id: str, int
:return: None
"""
id = id if id else self.ident_func()
if isinstance(id, (str,)):
id = int(id)
assert isinstance(id, (int,)), '[id] should be of int type'
if id in self:
with self.lock:
del self.objects[id]
return
raise AttributeError('Unable to find object with id {0}'.format(id))
def get(self, name: str):
"""
This method is a read access descriptor.
get() invokes each time thread want to get local context
object attribute value.
Thread should be registered before calling this method or
AttributeError will be raised.
:param name: str
:return: any
"""
id = self.ident_func()
if id in self:
attrs = self.objects[id]
return attrs.get(name)
raise AttributeError ('No local context object found for process with id {0}'.format(id))
def set(self, obj, name, value):
"""
This method is a write access descriptor.
set() invokes each time thread want to set local context
object attribute value.
Consider that even unregistered threads can set their local
context object value - set() will automatically register such
thread using register() method.
Object which call this method need to patch it's own __dict__
which leads to the need of 'obj' argument; also it needed
to restrict access to __dict__ and __name__ attributes.
:param obj:
:param name:
:param value:
:return: None
"""
id = self.ident_func()
if name == '__dict__' or name == '__name__':
raise AttributeError('{} object attribute {} is read-only'.format(type(obj).__name__, name))
if id in self:
attrs = self.objects[id]
attrs[name] = value
else:
attrs = dict()
attrs[name] = value
old_attrs = object.__getattribute__(obj, '__dict__')
for k in self._get_snames(obj):
attrs[k] = old_attrs[k]
self.register(attrs, id)
# special attributes patch
object.__setattr__(obj, '__dict__', attrs)
object.__setattr__(obj, '__name__', type(obj).__name__)
cls = type(obj)
if cls.__init__ is not object.__init__:
args, kwargs = object.__getattribute__(obj, '_{}__largs'.format(type(obj).__name__))
cls.__init__(obj, self, *args, **kwargs)
def delete(self, name: str):
"""
This method is delete access descriptor.
It invokes when thread is calling del operator on local
context object attribute.
:param name: str
:return: None
"""
id = self.ident_func()
if id in self:
attrs = self.objects[id]
if name in attrs.keys() and (name != '__name__' and name != '__dict__'):
with self.lock:
del attrs[name]
return
raise KeyError('{}'.format(name))
def _get_ident(self):
"""
Return method to identify current (caller) thread.
:return: function
"""
return get_ident
def _get_dict(self):
"""
This method filter context object __dict__ attribute
by removing special methods.
It's needed to make thread retrieve only useful attributes
without local context implementation helpers
:return: dict
"""
id = self.ident_func()
if id in self:
attrs = self.objects[id]
try:
name = attrs['__name__']
except KeyError:
raise AttributeError('Unable to retrieve class name') from None
special_methods = self._get_snames(name)
return {k: v for k, v in
filter(lambda item: item if item[0] not in special_methods
and not item[0].startswith('__') else None, attrs.items())}
def _get_snames(self, obj: object):
"""
This method builds the list of special object attributes
:param obj: str or instance
:return: list
"""
if isinstance(obj, (str,)):
name = obj
else:
try:
cls = object.__getattribute__(obj, '__class__')
name = cls.__name__
except:
raise TypeError('Object is neither instance nor string. Cannot obtain __name__')
return ['_{}__{}'.format(name, method) for method in self.SPECIAL_METHODS]