File: contextlocals.rst

package info (click to toggle)
python-pecan 1.5.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,296 kB
  • sloc: python: 10,389; makefile: 131; sh: 17
file content (55 lines) | stat: -rw-r--r-- 2,267 bytes parent folder | download | duplicates (7)
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
.. _contextlocals:


Context/Thread-Locals vs. Explicit Argument Passing
===================================================
In any pecan application, the module-level ``pecan.request`` and
``pecan.response`` are proxy objects that always refer to the request and
response being handled in the current thread.

This `thread locality` ensures that you can safely access a global reference to
the current request and response in a multi-threaded environment without
constantly having to pass object references around in your code; it's a feature
of pecan that makes writing traditional web applications easier and less
verbose.

Some people feel thread-locals are too implicit or magical, and that explicit
reference passing is much clearer and more maintainable in the long run.
Additionally, the default implementation provided by pecan uses
:func:`threading.local` to associate these context-local proxy objects with the
`thread identifier` of the current server thread.  In asynchronous server
models - where lots of tasks run for short amounts of time on
a `single` shared thread - supporting this mechanism involves monkeypatching
:func:`threading.local` to behave in a greenlet-local manner.

Disabling Thread-Local Proxies
------------------------------

If you're certain that you `do not` want to utilize context/thread-locals in
your project, you can do so by passing the argument
``use_context_locals=False`` in your application's configuration file::

    app = {
        'root': 'project.controllers.root.RootController',
        'modules': ['project'],
        'static_root': '%(confdir)s/public',
        'template_path': '%(confdir)s/project/templates',
        'debug': True,
        'use_context_locals': False
    }

Additionally, you'll need to update **all** of your pecan controllers to accept
positional arguments for the current request and response::

    class RootController(object):

        @pecan.expose('json')
        def index(self, req, resp):
            return dict(method=req.method) # path: /

        @pecan.expose()
        def greet(self, req, resp, name):
            return name  # path: /greet/joe

It is *imperative* that the request and response arguments come **after**
``self`` and before any positional form arguments.