-
Notifications
You must be signed in to change notification settings - Fork 10
/
local.py
92 lines (60 loc) · 2.3 KB
/
local.py
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
# encoding: utf-8
# ## Imports
from __future__ import unicode_literals
from threading import local
from marrow.package.loader import traverse
# ## Module Globals
# Standard logger object.
log = __import__('logging').getLogger(__name__)
# ## Extension
class ThreadLocalExtension(object):
"""Provide the current context as a thread local global.
This provides a convienent "superglobal" variable where you can store per-thread data.
While the context itself is cleaned up after each call, any data you add won't be. These are not request-locals.
"""
first = True
provides = ['local', 'threadlocal']
def __init__(self, where='web.core:local'):
"""Initialize thread local storage for the context.
By default the `local` object in the `web.core` package will be populated as a `threading.local` pool. The
context, during a request, can then be accessed as `web.core.local.context`. Your own extensions can add
additional arbitrary data to this pool.
"""
super(ThreadLocalExtension, self).__init__()
if __debug__:
log.debug("Initalizing ThreadLocal extension.")
self.where = where
self.local = None
self.preserve = False
def _lookup(self):
module, _, name = self.where.rpartition(':')
module = traverse(__import__(module), '.'.join(module.split('.')[1:]), separator='.')
return module, name
def start(self, context):
module, name = self._lookup()
if __debug__:
log.debug("Preparing thread local storage and assigning main thread application context.")
if hasattr(module, name):
self.local = getattr(module, name)
self.preserve = True
else:
self.local = local()
setattr(module, name, self.local)
self.local.context = context # Main thread application context.
def stop(self, context):
self.local = None
if __debug__:
log.debug("Cleaning up thread local storage.")
if not self.preserve:
module, name = self._lookup()
delattr(module, name)
def prepare(self, context):
"""Executed prior to processing a request."""
if __debug__:
log.debug("Assigning thread local request context.")
self.local.context = context
def done(self, result):
"""Executed after the entire response has been sent to the client."""
if __debug__:
log.debug("Cleaning up thread local request context.")
del self.local.context