Context Locals

优质
小牛编辑
125浏览
2023-12-01

Sooner or later you have some things you want to have in every single view or helper function or whatever. In PHP the way to go are global variables. However, that isn’t possible in WSGI applications without a major drawback: As soon as you operate on the global namespace your application isn’t thread-safe any longer.

The Python standard library comes with a utility called “thread locals”. A thread local is a global object in which you can put stuff in and get back later in a thread-safe way. That means whenever you set or get an object on a thread local object, the thread local object checks in which thread you are and retrieves the correct value.

This, however, has a few disadvantages. For example, besides threads there are other ways to handle concurrency in Python. A very popular approach is greenlets. Also, whether every request gets its own thread is not guaranteed in WSGI. It could be that a request is reusing a thread from before, and hence data is left in the thread local object.

Here’s a simple example of how one could use werkzeug.local:

from werkzeug.local import Local, LocalManager

local = Local()
local_manager = LocalManager([local])

def application(environ, start_response):
    local.request = request = Request(environ)
    ...

application = local_manager.make_middleware(application)

This binds the request to local.request. Every other piece of code executed after this assignment in the same context can safely access local.request and will get the same request object. The make_middleware method on the local manager ensures that all references to the local objects are cleared up after the request.

The same context means the same greenlet (if you’re using greenlets) in the same thread and same process.

If a request object is not yet set on the local object and you try to access it, you will get an AttributeError. You can use getattr to avoid that:

def get_request():
    return getattr(local, 'request', None)

This will try to get the request or return None if the request is not (yet?) available.

Note that local objects cannot manage themselves, for that you need a local manager. You can pass a local manager multiple locals or add additionals later by appending them to manager.locals and everytime the manager cleans up it will clean up all the data left in the locals for this context.

werkzeug.local.release_local(local)

Releases the contents of the local for the current context. This makes it possible to use locals without a manager.

Example:

>>> loc = Local()
>>> loc.foo = 42
>>> release_local(loc)
>>> hasattr(loc, 'foo')
False

With this function one can release Local objects as well as werkzeug.local.LocalStack" title="werkzeug.local.LocalStack objects. However it is not possible to release data held by proxies that way, one always has to retain a reference to the underlying local object in order to be able to release it.

0.6.1 新版功能.

class werkzeug.local.LocalManager(locals=None, ident_func=None)

Local objects cannot manage themselves. For that you need a local manager. You can pass a local manager multiple locals or add them later by appending them to manager.locals. Everytime the manager cleans up it, will clean up all the data left in the locals for this context.

The ident_func parameter can be added to override the default ident function for the wrapped locals.

在 0.6.1 版更改: Instead of a manager the werkzeug.local.release_local" title="werkzeug.local.release_local function can be used as well.

在 0.7 版更改: ident_func was added.

cleanup()

Manually clean up the data in the locals for this context. Call this at the end of the request or use make_middleware().

get_ident()

Return the context identifier the local objects use internally for this context. You cannot override this method to change the behavior but use it to link other context local objects (such as SQLAlchemy’s scoped sessions) to the Werkzeug locals.

在 0.7 版更改: Yu can pass a different ident function to the local manager that will then be propagated to all the locals passed to the constructor.

make_middleware(app)

Wrap a WSGI application so that cleaning up happens after request end.

middleware(func)

Like make_middleware but for decorating functions.

Example usage:

@manager.middleware
def application(environ, start_response):
    ...

The difference to make_middleware is that the function passed will have all the arguments copied from the inner application (name, docstring, module).

class werkzeug.local.LocalStack

This class works similar to a Local but keeps a stack of objects instead. This is best explained with an example:

>>> ls = LocalStack()
>>> ls.push(42)
>>> ls.top
42
>>> ls.push(23)
>>> ls.top
23
>>> ls.pop()
23
>>> ls.top
42

They can be force released by using a werkzeug.local.LocalManager" title="werkzeug.local.LocalManager or with the werkzeug.local.release_local" title="werkzeug.local.release_local function but the correct way is to pop the item from the stack after using. When the stack is empty it will no longer be bound to the current context (and as such released).

By calling the stack without arguments it returns a proxy that resolves to the topmost item on the stack.

0.6.1 新版功能.

pop()

Removes the topmost item from the stack, will return the old value or None if the stack was already empty.

push(obj)

Pushes a new item to the stack

top

The topmost item on the stack. If the stack is empty, None is returned.

class werkzeug.local.LocalProxy(local, name=None)

Acts as a proxy for a werkzeug local. Forwards all operations to a proxied object. The only operations not supported for forwarding are right handed operands and any kind of assignment.

Example usage:

from werkzeug.local import Local
l = Local()

# these are proxies
request = l('request')
user = l('user')


from werkzeug.local import LocalStack
_response_local = LocalStack()

# this is a proxy
response = _response_local()

Whenever something is bound to l.user / l.request the proxy objects will forward all operations. If no object is bound a RuntimeError will be raised.

To create proxies to Local or werkzeug.local.LocalStack" title="werkzeug.local.LocalStack objects, call the object as shown above. If you want to have a proxy to an object looked up by a function, you can (as of Werkzeug 0.6.1) pass a function to the werkzeug.local.LocalProxy" title="werkzeug.local.LocalProxy constructor:

session = LocalProxy(lambda: get_current_request().session)

在 0.6.1 版更改: The class can be instanciated with a callable as well now.

Keep in mind that repr() is also forwarded, so if you want to find out if you are dealing with a proxy you can do an isinstance() check:

>>> from werkzeug.local import LocalProxy
>>> isinstance(request, LocalProxy)
True

You can also create proxy objects by hand:

from werkzeug.local import Local, LocalProxy
local = Local()
request = LocalProxy(local, 'request')
_get_current_object()

Return the current object. This is useful if you want the real object behind the proxy at a time for performance reasons or because you want to pass the object into a different context.