module documentation

Mixin for async lifecycle management for mixin-composed classes.

Concepts

Resource
A single lifecycle-decorated async-generator method. Code before yield initializes it; code after yield shuts it down. A resource is the unit of ordering: after, before and requires refer to resources, not classes.
Resource mixin
A plain class (does not inherit LifecycleMixin) that provides one or more resources.
Composed class
The concrete class that inherits its resource mixins and LifecycleMixin. This is the only class that should inherit LifecycleMixin.
Ordering
Resources are initialized in dependency order and shut down in the exact reverse order. Order is resolved once, in LifecycleMixin.__init_subclass__, from after / before arguments of lifecycle-decorated methods. requires is checked at the same point but imposes no ordering.
Class LifecycleMixin Mixin managing the lifecycle of classes with lifecycle decorated methods.
Function lifecycle Mark an async generator method as a resource.
Function _build_edges Validate required resources and build lifecycle ordering edges.
Function _collect_funcs Collect all methods from given class decorated with @lifecycle.
Constant _ATTR_LIFECYCLE_AFTER Undocumented
Constant _ATTR_LIFECYCLE_BEFORE Undocumented
Constant _ATTR_LIFECYCLE_REQUIRES Undocumented
Constant _ATTR_LIFECYCLE_RESOURCE Undocumented
Constant _LOGGER Undocumented
Type Variable _LifecycleFunc Undocumented
Type Alias _Func Undocumented
Variable _FuncOrFuncs Undocumented
@overload
def lifecycle(_func: _LifecycleFunc) -> _LifecycleFunc:
@overload
def lifecycle(_func: None = None, *, after: _FuncOrFuncs = (), before: _FuncOrFuncs = (), requires: _FuncOrFuncs = ()) -> Callable[[_LifecycleFunc], _LifecycleFunc]:

Mark an async generator method as a resource.

Code before yield runs during initialization; code after yield runs during shutdown, in reverse lifecycle order.

Parameters
_func:_LifecycleFunc | NoneUndocumented
after:_FuncOrFuncsOrdering constraint. If the referenced resources are present in the final composed class, they are started before and stopped after this resource. Silently ignored for resources that are absent.
before:_FuncOrFuncsOrdering constraint. If the referenced resources are present in the final composed class, this resource is started before and stopped after them. Silently ignored for resources that are absent.
requires:_FuncOrFuncsPresence constraint. All referenced resources must be present in the final composed class. This does not impose an ordering constraint. Checked by LifecycleMixin.__init_subclass__.
Returns
_LifecycleFunc | Callable[[_LifecycleFunc], _LifecycleFunc]Undocumented
def _build_edges(funcs: set[_Func]) -> dict[_Func, set[_Func]]:

Validate required resources and build lifecycle ordering edges.

def _collect_funcs(cls: type) -> set[_Func]:

Collect all methods from given class decorated with @lifecycle.

_ATTR_LIFECYCLE_AFTER: str =

Undocumented

Value
'_lifecycle_after'
_ATTR_LIFECYCLE_BEFORE: str =

Undocumented

Value
'_lifecycle_before'
_ATTR_LIFECYCLE_REQUIRES: str =

Undocumented

Value
'_lifecycle_requires'
_ATTR_LIFECYCLE_RESOURCE: str =

Undocumented

Value
'_is_lifecycle_resource'
_LOGGER =

Undocumented

Value
structlog.get_logger(('opensmi.' + __name__))
_LifecycleFunc =

Undocumented

Value
TypeVar('_LifecycleFunc',
        bound=Callable[..., AsyncGenerator[None, None]])
_Func =

Undocumented

Value
Callable[..., AsyncGenerator[None, None]]
_FuncOrFuncs =

Undocumented