Component
elva.component
Module for a generic asynchronous app component.
Classes:
-
Component–Generic asynchronous app component class.
Functions:
-
create_component_state–Create a
Flagenumeration with the default flagsNONEandRUNNINGforComponents.
Attributes:
-
ComponentState–The default component states.
ComponentState = create_component_state('ComponentState')
module-attribute
The default component states.
Component
Generic asynchronous app component class.
This class features graceful shutdown alongside annotated logging. It is used for writing providers, stores, parsers, renderers etc.
It supports explicit handling via the start
and stop method as well as the asynchronous
context manager protocol.
Methods:
-
subscribe–Get an object to listen on for differences in component state.
-
unsubscribe–Close and remove the memory object stream from the mapping of subscribers.
-
close–Run
unsubscribefrom all subscriptions. -
__del__–Destructor callback.
-
__aenter__–Asynchronous context manager enter callback.
-
__aexit__–Asynchronous context manager exit callback.
-
start–Start the component.
-
stop–Stop the component by cancelling all inner task groups.
-
before–Hook to run before the component signals that is running.
-
run–Hook to run after the component set the
RUNNINGstate. -
cleanup–Hook to run after the component's
stopmethod
Attributes:
-
log(Logger) –Logger instance to write logging messages to.
-
states(ComponentState) –Enumeration class holding all states the component can have.
-
state(ComponentState) –The current state of the component.
log
instance-attribute
Logger instance to write logging messages to.
_subscribers
instance-attribute
Mapping of receiving streams to their respective sending stream over which to publish state changes.
states
property
Enumeration class holding all states the component can have.
state
property
The current state of the component.
subscribe()
Get an object to listen on for differences in component state.
Returns:
-
MemoryObjectReceiveStream–the receiving end of an asynchronous memory object stream emitting tuple of deleted and added states.
Source code in src/elva/component.py
unsubscribe(recv)
Close and remove the memory object stream from the mapping of subscribers.
Parameters:
-
recv(MemoryObjectReceiveStream) –the receiving end of the memory object stream as returned by
subscribe.
Source code in src/elva/component.py
_change_state(from_state, to_state)
Replace a state with another state within the current component state.
The special state NONE can be used as an identity, i.e. no-op, flag.
Parameters:
Source code in src/elva/component.py
close()
__del__()
_get_start_lock()
__aenter__()
async
Asynchronous context manager enter callback.
It starts the _run coroutine
in a task group.
Source code in src/elva/component.py
__aexit__(exc_type, exc_value, exc_tb)
async
Asynchronous context manager exit callback.
It stops this component by cancelling the inner task group scope.
Parameters:
-
exc_type(None | Exception) –the type of the exception causing the exit.
-
exc_value(None | str) –the value of the exception causing the exit.
-
exc_tb(None | TracebackType) –the traceback of the exception causing the exit.
Returns:
-
Awaitable–an awaitable from the exit stacks own exit callback.
Source code in src/elva/component.py
_run(task_status=TASK_STATUS_IGNORED)
async
Hook handling the run method gracefully.
Raises:
-
get_cancelled_exc_class–the exception from cancellation.
Parameters:
-
task_status(TaskStatus[None], default:TASK_STATUS_IGNORED) –an optional task status object to call
startedon when the task is considered to have started.
Source code in src/elva/component.py
start(task_status=TASK_STATUS_IGNORED)
async
Start the component.
Parameters:
-
task_status(TaskStatus[None], default:TASK_STATUS_IGNORED) –an optional task status object to call
startedon when the task is considered to have started.
Source code in src/elva/component.py
stop()
async
Stop the component by cancelling all inner task groups.
before()
async
Hook to run before the component signals that is running.
In here, one would define initializing steps necessary for the component to run.
This method must return, otherwise the component will not set the
RUNNING state.
It is defined as a no-op and supposed to be implemented in the inheriting class.
Source code in src/elva/component.py
run()
async
Hook to run after the component set the RUNNING state.
In here, one would define the main functionality of the component. This method may run indefinitely or return. The component is kept running regardless.
It is defined as a no-op and supposed to be implemented in the inheriting class.
Source code in src/elva/component.py
cleanup()
async
Hook to run after the component's stop method
has been called and before it sets its state to NONE.
In here, one would define cleanup tasks such as closing connections. This method must return, otherwise the component will not stop.
It is defined as a no-op and supposed to be implemented in the inheriting class.
Source code in src/elva/component.py
create_component_state(name, additional_states=None)
Create a Flag enumeration with the default flags NONE and RUNNING for Components.
Parameters:
-
name(str) –the states class name.
-
additional_states(None | Iterable[str], default:None) –states to include next to the default ones.
Returns:
-
Flag–component states as flag enumeration.