utils.threads

Threading primitives and utilities.

Module Contents

Classes

bgThread(self,name=None,**kwargs) Background service thread.
Local(self) Local object.
_LocalStack(self) Local stack.
LocalManager(self,locals=None,ident_func=None) Local objects cannot manage themselves.
_FastLocalStack(self)

Functions

default_socket_timeout(timeout) Context temporarily setting the default socket timeout.
release_local(local) Release the contents of the local for the current context.
default_socket_timeout(timeout)

Context temporarily setting the default socket timeout.

class bgThread(name=None, **kwargs)

Background service thread.

__init__(name=None, **kwargs)
body()
on_crash(msg, *fmt, **kwargs)
run()
_set_stopped()
stop()

Graceful shutdown.

release_local(local)

Release the contents of the local for the current context.

This makes it possible to use locals without a manager.

With this function one can release Local objects as well as StackLocal objects. However it’s 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.

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

Local object.

__init__()
__iter__()
__call__(proxy)

Create a proxy for a name.

__release_local__()
__getattr__(name)
__setattr__(name, value)
__delattr__(name)
class _LocalStack

Local stack.

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 LocalManager or with the 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 will return a proxy that resolves to the topmost item on the stack.

__init__()
__release_local__()
_get__ident_func__()
_set__ident_func__(value)
__call__()
push(obj)

Push a new item to the stack.

pop()

Remove the topmost item from the stack.

Note:
Will return the old value or None if the stack was already empty.
__len__()
stack()
top()

The topmost item on the stack.

Note:
If the stack is empty, None is returned.
class 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. Every time 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.

__init__(locals=None, ident_func=None)
get_ident()

Return context identifier.

This is the indentifer 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.

cleanup()

Manually clean up the data in the locals for this context.

Call this at the end of the request or use make_middleware().

__repr__()
class _FastLocalStack
__init__()
top()
__len__()