Utilities to deal with context managers.
New in version 0.6.
Helper which manages context dependant stacks.
A common API pattern is using context managers to change options; those options are internally stored on a stack.
However larger applications have multiple execution contexts such as processes, threads and/or coroutines/greenthreads and such an API becomes a problem as each modification of the stack becomes visible to every execution context.
This helper allows you to make stack operations local to the current execution context, ensuring that the stack remains the same in other contexts unless you want it to change there.
As applications tend to have very different requirements and use different contexts each is implemented in a separate mixin, this way it easily possible to create a ContextStackManager for your needs.
Assuming your application uses threads and eventlet for greenthreads you would create a ContextStackManager like this:
class ContextStackManager(
ContextStackManagerEventletMixin,
ContextStackManagerThreadMixin,
ContextStackManagerBase
):
pass
Greenthreads are executed in a thread, whereas threads are executed in the application thread (handled by the base class) this is why ContextStackManager inherits from these classes exactly in this order.
Currently available mixins are:
A ContextStackManagerBase mixin providing thread context support.
Pops and returns an object from the thread stack.
Pushes the given object onto the thread stack.
A ContextStackManagerBase mixin providing coroutine/greenthread context support using eventlet.
Pops and returns an object from the coroutine stack.
Pushes the given object onto the coroutine stack.