Yona provides a specialized infrastructure for managing resources, including their initialization and finalization, with guaranteed ordering and error handling.
This feature is called context managers and it allows for generic, extensible management of local contexts. Context managers are used in conjunction with the
Context managers provide the necessary gear for dealing with resource management is a structured way, that can be used together with the
with expression. They provide a way for Yona to initialize and finalize resources. Resources may be explicitly or implicitly named direct access within the scope of the
Context managers store their data in a local dictionary, so that they can be used from any threads (this is transparent for programmers, who need not to be aware of this actually).
Context manager is a triple of
(name, wrapper, data), where:
nameis the default name of the alias, used as a key in the local context dictionary.
wrapperwrapper is a 2-arg function - taking the context manager tuple and a callback that is called from within this wrapper function and of which result is returned as a result of this wrapper. The wrapper can perform initialization and finalization of resources around calling the callback.
datais the actual data, such as transaction object, file object or whatever other resource data to be managed by the context manager.
Lifecycle of Context Managers¶
Context manager is executed in this order:
- the context manager tuple is stored to the local context dictionary
wrapperfunction is executed, expected to initialize necessary resources, then call the callback passed into this function and finally resources get disposed after they've been used
- if body expression raises an exception, the
leavefunction is still called regardless
Whether an exception is raised or not within the context manager execution is not important for its lifecycle. Context managers ensure that proper initialization and finalization of resources always takes place and for this reason they are the recommended way for handling this sort of thing