File: registry.html

package info (click to toggle)
paste 1.7.5.1-4.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,188 kB
  • sloc: python: 20,660; makefile: 45
file content (297 lines) | stat: -rw-r--r-- 17,000 bytes parent folder | download | duplicates (3)
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


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>paste.registry – Manage thread-local request-specific objects &mdash; Paste v1.7.5 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '1.7.5',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="top" title="Paste v1.7.5 documentation" href="../index.html" />
 
<link rel="stylesheet" type="text/css"
 href="../_static/paste.css.html">

  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../index.html">Paste v1.7.5 documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-paste.registry">
<span id="paste-registry-manage-thread-local-request-specific-objects"></span><h1><a class="reference internal" href="#module-paste.registry"><tt class="xref py py-mod docutils literal"><span class="pre">paste.registry</span></tt></a> &#8211; Manage thread-local request-specific objects<a class="headerlink" href="#module-paste.registry" title="Permalink to this headline">¶</a></h1>
<p>Registry for handling request-local module globals sanely</p>
<p>Dealing with module globals in a thread-safe way is good if your
application is the sole responder in a thread, however that approach fails
to properly account for various scenarios that occur with WSGI applications
and middleware.</p>
<p>What is actually needed in the case where a module global is desired that
is always set properly depending on the current request, is a stacked
thread-local object. Such an object is popped or pushed during the request
cycle so that it properly represents the object that should be active for
the current request.</p>
<p>To make it easy to deal with such variables, this module provides a special
StackedObjectProxy class which you can instantiate and attach to your
module where you&#8217;d like others to access it. The object you&#8217;d like this to
actually &#8220;be&#8221; during the request is then registered with the
RegistryManager middleware, which ensures that for the scope of the current
WSGI application everything will work properly.</p>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c">#yourpackage/__init__.py</span>

<span class="kn">from</span> <span class="nn">paste.registry</span> <span class="kn">import</span> <span class="n">RegistryManager</span><span class="p">,</span> <span class="n">StackedObjectProxy</span>
<span class="n">myglobal</span> <span class="o">=</span> <span class="n">StackedObjectProxy</span><span class="p">()</span>

<span class="c">#wsgi app stack</span>
<span class="n">app</span> <span class="o">=</span> <span class="n">RegistryManager</span><span class="p">(</span><span class="n">yourapp</span><span class="p">)</span>

<span class="c">#inside your wsgi app</span>
<span class="k">class</span> <span class="nc">yourapp</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">environ</span><span class="p">,</span> <span class="n">start_response</span><span class="p">):</span>
        <span class="n">obj</span> <span class="o">=</span> <span class="n">someobject</span>  <span class="c"># The request-local object you want to access</span>
                          <span class="c"># via yourpackage.myglobal</span>
        <span class="k">if</span> <span class="n">environ</span><span class="o">.</span><span class="n">has_key</span><span class="p">(</span><span class="s">&#39;paste.registry&#39;</span><span class="p">):</span>
            <span class="n">environ</span><span class="p">[</span><span class="s">&#39;paste.registry&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">myglobal</span><span class="p">,</span> <span class="n">obj</span><span class="p">)</span>
</pre></div>
</div>
<p>You will then be able to import yourpackage anywhere in your WSGI app or in
the calling stack below it and be assured that it is using the object you
registered with Registry.</p>
<p>RegistryManager can be in the WSGI stack multiple times, each time it
appears it registers a new request context.</p>
<div class="section" id="performance">
<h2>Performance<a class="headerlink" href="#performance" title="Permalink to this headline">¶</a></h2>
<p>The overhead of the proxy object is very minimal, however if you are using
proxy objects extensively (Thousands of accesses per request or more), there
are some ways to avoid them. A proxy object runs approximately 3-20x slower
than direct access to the object, this is rarely your performance bottleneck
when developing web applications.</p>
<p>Should you be developing a system which may be accessing the proxy object
thousands of times per request, the performance of the proxy will start to
become more noticeable. In that circumstance, the problem can be avoided by
getting at the actual object via the proxy with the <tt class="docutils literal"><span class="pre">_current_obj</span></tt> function:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c">#sessions.py</span>
<span class="n">Session</span> <span class="o">=</span> <span class="n">StackedObjectProxy</span><span class="p">()</span>
<span class="c"># ... initialization code, etc.</span>

<span class="c"># somemodule.py</span>
<span class="kn">import</span> <span class="nn">sessions</span>

<span class="k">def</span> <span class="nf">somefunc</span><span class="p">():</span>
    <span class="n">session</span> <span class="o">=</span> <span class="n">sessions</span><span class="o">.</span><span class="n">Session</span><span class="o">.</span><span class="n">_current_obj</span><span class="p">()</span>
    <span class="c"># ... tons of session access</span>
</pre></div>
</div>
<p>This way the proxy is used only once to retrieve the object for the current
context and the overhead is minimized while still making it easy to access
the underlying object. The <tt class="docutils literal"><span class="pre">_current_obj</span></tt> function is preceded by an
underscore to more likely avoid clashing with the contained object&#8217;s
attributes.</p>
<p><strong>NOTE:</strong> This is <em>highly</em> unlikely to be an issue in the vast majority of
cases, and requires incredibly large amounts of proxy object access before
one should consider the proxy object to be causing slow-downs. This section
is provided solely in the extremely rare case that it is an issue so that a
quick way to work around it is documented.</p>
</div>
<div class="section" id="module-contents">
<h2>Module Contents<a class="headerlink" href="#module-contents" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="paste.registry.StackedObjectProxy">
<em class="property">class </em><tt class="descclassname">paste.registry.</tt><tt class="descname">StackedObjectProxy</tt><big>(</big><em>default=&lt;class 'paste.registry.NoDefault'&gt;</em>, <em>name='Default'</em><big>)</big><a class="headerlink" href="#paste.registry.StackedObjectProxy" title="Permalink to this definition">¶</a></dt>
<dd><p>Track an object instance internally using a stack</p>
<p>The StackedObjectProxy proxies access to an object internally using a
stacked thread-local. This makes it safe for complex WSGI environments
where access to the object may be desired in multiple places without
having to pass the actual object around.</p>
<p>New objects are added to the top of the stack with _push_object while
objects can be removed with _pop_object.</p>
</dd></dl>

<dl class="class">
<dt id="paste.registry.Registry">
<em class="property">class </em><tt class="descclassname">paste.registry.</tt><tt class="descname">Registry</tt><a class="headerlink" href="#paste.registry.Registry" title="Permalink to this definition">¶</a></dt>
<dd><p>Track objects and stacked object proxies for removal</p>
<p>The Registry object is instantiated a single time for the request no
matter how many times the RegistryManager is used in a WSGI stack. Each
RegistryManager must call <tt class="docutils literal"><span class="pre">prepare</span></tt> before continuing the call to
start a new context for object registering.</p>
<p>Each context is tracked with a dict inside a list. The last list
element is the currently executing context. Each context dict is keyed
by the id of the StackedObjectProxy instance being proxied, the value
is a tuple of the StackedObjectProxy instance and the object being
tracked.</p>
</dd></dl>

<dl class="class">
<dt id="paste.registry.RegistryManager">
<em class="property">class </em><tt class="descclassname">paste.registry.</tt><tt class="descname">RegistryManager</tt><big>(</big><em>application</em>, <em>streaming=False</em><big>)</big><a class="headerlink" href="#paste.registry.RegistryManager" title="Permalink to this definition">¶</a></dt>
<dd><p>Creates and maintains a Registry context</p>
<p>RegistryManager creates a new registry context for the registration of
StackedObjectProxy instances. Multiple RegistryManager&#8217;s can be in a
WSGI stack and will manage the context so that the StackedObjectProxies
always proxy to the proper object.</p>
<p>The object being registered can be any object sub-class, list, or dict.</p>
<p>Registering objects is done inside a WSGI application under the
RegistryManager instance, using the <tt class="docutils literal"><span class="pre">environ['paste.registry']</span></tt>
object which is a Registry instance.</p>
</dd></dl>

<dl class="class">
<dt id="paste.registry.StackedObjectRestorer">
<em class="property">class </em><tt class="descclassname">paste.registry.</tt><tt class="descname">StackedObjectRestorer</tt><a class="headerlink" href="#paste.registry.StackedObjectRestorer" title="Permalink to this definition">¶</a></dt>
<dd><p>Track StackedObjectProxies and their proxied objects for automatic
restoration within EvalException&#8217;s interactive debugger.</p>
<p>An instance of this class tracks all StackedObjectProxy state in existence
when unexpected exceptions are raised by WSGI applications housed by
EvalException and RegistryManager. Like EvalException, this information is
stored for the life of the process.</p>
<p>When an unexpected exception occurs and EvalException is present in the
WSGI stack, save_registry_state is intended to be called to store the
Registry state and enable automatic restoration on all currently registered
StackedObjectProxies.</p>
<p>With restoration enabled, those StackedObjectProxies&#8217; _current_obj
(overwritten by _current_obj_restoration) method&#8217;s strategy is modified:
it will return its appropriate proxied object from the restorer when
a restoration context is active in the current thread.</p>
<p>The StackedObjectProxies&#8217; _push/pop_object methods strategies are also
changed: they no-op when a restoration context is active in the current
thread (because the pushing/popping work is all handled by the
Registry/restorer).</p>
<p>The request&#8217;s Registry objects&#8217; reglists are restored from the restorer
when a restoration context begins, enabling the Registry methods to work
while their changes are tracked by the restorer.</p>
<p>The overhead of enabling restoration is negligible (another threadlocal
access for the changed StackedObjectProxy methods) for normal use outside
of a restoration context, but worth mentioning when combined with
StackedObjectProxies normal overhead. Once enabled it does not turn off,
however:</p>
<p>o Enabling restoration only occurs after an unexpected exception is
detected. The server is likely to be restarted shortly after the exception
is raised to fix the cause</p>
<p>o StackedObjectRestorer is only enabled when EvalException is enabled (not
on a production server) and RegistryManager exists in the middleware
stack</p>
</dd></dl>

<dl class="function">
<dt id="paste.registry.make_registry_manager">
<tt class="descclassname">paste.registry.</tt><tt class="descname">make_registry_manager</tt><big>(</big><em>app</em>, <em>global_conf</em><big>)</big><a class="headerlink" href="#paste.registry.make_registry_manager" title="Permalink to this definition">¶</a></dt>
<dd><p>Creates and maintains a Registry context</p>
<p>RegistryManager creates a new registry context for the registration of
StackedObjectProxy instances. Multiple RegistryManager&#8217;s can be in a
WSGI stack and will manage the context so that the StackedObjectProxies
always proxy to the proper object.</p>
<p>The object being registered can be any object sub-class, list, or dict.</p>
<p>Registering objects is done inside a WSGI application under the
RegistryManager instance, using the <tt class="docutils literal"><span class="pre">environ['paste.registry']</span></tt>
object which is a Registry instance.</p>
</dd></dl>

</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
<h3><a href="http://pythonpaste.org/" class="invisible-link">Python Paste</a></h3>

<ul>
<li><a href="http://trac.pythonpaste.org">Issue tracker</a></li>
<li><a href="http://pythonpaste.org/">Paste core</a></li>
<li><a href="http://pythonpaste.org/webob/">WebOb</a></li>
<li><a href="http://pythonpaste.org/deploy/">Paste Deploy</a></li>
<li><a href="http://pythonpaste.org/script/">Paste Script</a></li>
<li><a href="http://pythonpaste.org/webtest/">WebTest</a></li>
<li><a href="http://pythonpaste.org/scripttest/">ScriptType</a></li>
<li><a href="http://pythonpaste.org/initools/">INITools</a></li>
<li><a href="http://pythonpaste.org/tempita/">Tempita</a></li>
<li><a href="http://pythonpaste.org/waitforit/">WaitForIt</a></li>
<li><a href="http://pythonpaste.org/wphp/">WPHP</a></li>
<li><a href="http://pythonpaste.org/wsgifilter/">WSGIFilter</a></li>
<li><a href="http://pythonpaste.org/wsgiproxy/">WSGIProxy</a></li>
</ul>


  <h3><a href="../index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#"><tt class="docutils literal"><span class="pre">paste.registry</span></tt> &#8211; Manage thread-local request-specific objects</a><ul>
<li><a class="reference internal" href="#performance">Performance</a></li>
<li><a class="reference internal" href="#module-contents">Module Contents</a></li>
</ul>
</li>
</ul>


  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="../_sources/modules/registry.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../index.html">Paste v1.7.5 documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2008, Ian Bicking.
      Last updated on Sep 14, 2010.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.1.
    </div>
  </body>
</html>