Context Manager Handling

Utilities to deal with context managers.

New in version 0.6.

class brownie.context.ContextStackManagerBase(_object_cache_maxsize=256)[source]

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:

iter_current_stack()[source]

Returns an iterator over the items in the ‘current’ stack, ordered from top to bottom.

push_application(obj)[source]

Pushes the given object onto the application stack.

pop_application()[source]

Pops and returns an object from the application stack.

class brownie.context.ContextStackManagerThreadMixin(*args, **kwargs)[source]

A ContextStackManagerBase mixin providing thread context support.

pop_thread()

Pops and returns an object from the thread stack.

push_thread(obj)

Pushes the given object onto the thread stack.

class brownie.context.ContextStackManagerEventletMixin(*args, **kwargs)[source]

A ContextStackManagerBase mixin providing coroutine/greenthread context support using eventlet.

pop_coroutine()

Pops and returns an object from the coroutine stack.

push_coroutine(obj)

Pushes the given object onto the coroutine stack.

Navigation

Documentation overview

This Page

Fork me on GitHub