async2v.components.base module

Summary

Classes:

BareComponent Unmanaged component
Component Abstract base class for all components
ContainerMixin Mixin allowing components to have subcomponents.
EventDrivenComponent Runs when trigger events are received
IteratingComponent Runs at a fixed frame rate
SubComponent Dependent component

Reference

class Component

Bases: async2v.components.base._BaseComponent

Abstract base class for all components

For building components, use one of the available subclasses:

id

Human readable component id, created from class name and ascending index (e.g. VideoSource0).

Return type:str
graph_colors

Override to provide custom colors for the application graph that can be generated with the graph command.

Return type:Tuple[str, str]
Returns:(background color, foreground color) as hex RGB string (e.g. #808080)
shutdown()

Trigger graceful shutdown of the application.

coroutine cleanup(self)

Can be overridden to perform cleanup before the component is shut down.

This method is run before unregistering the component. The inputs & outputs are still wired and can be used, but other components might not be reacting to those events any more as they have already been shut down.

Return type:None
coroutine setup(self)

Can be overridden to perform setup before processing starts.

This method is run after registration and app startup, i.e. all inputs & outputs are wired and can be used.

Return type:None
class IteratingComponent

Bases: async2v.components.base.Component

Runs at a fixed frame rate

The iterating component is triggered by the framework regularly. It tries to run the process method in a way that the target frame rate is achieved. However, as other factors like runtime of other components are involved and the available processing time is limited, you should not rely on an exact frame rate.

coroutine setup(self)

Can be overridden to perform setup before processing starts.

This method is run after registration and app startup, i.e. all inputs & outputs are wired and can be used.

Return type:None
coroutine cleanup(self)

Can be overridden to perform cleanup before the component is shut down.

This method is run before unregistering the component. The inputs & outputs are still wired and can be used, but other components might not be reacting to those events any more as they have already been shut down.

Return type:None
target_fps

Must be overridden to specify the target processing rate in Hz.

This method is only called once during startup of the component. It is not possible to change the target frame rate during runtime.

Return type:int
coroutine process(self)

Must be overridden to implement the component’s core logic.

This method is called by the framework regularly at approximately the rate returned by target_fps.

It is never called before setup() or after cleanup() has been called for this component.

Return type:None
class EventDrivenComponent

Bases: async2v.components.base.Component

Runs when trigger events are received

An EventDrivenComponent needs to have at least one trigger field (a DoubleBufferedField like Latest or Buffer constructed with trigger=True). When one of the trigger fields receives an event, the component is marked for execution. Its process method will be invoked when the component runner is given control by the asyncio scheduler. That does not happen immediately - further events (also on different fields) can arrive between the trigger and the actual invocation of the process method. Those events don’t lead to additional invocations.

The guarantees by the framework are:

  • if this method has been called, at least one event has arrived on any of the trigger fields
  • if any of the trigger fields receives an event, a method invocation is scheduled and performed as soon as possible (if not already scheduled)
coroutine setup(self)

Can be overridden to perform setup before processing starts.

This method is run after registration and app startup, i.e. all inputs & outputs are wired and can be used.

Return type:None
coroutine cleanup(self)

Can be overridden to perform cleanup before the component is shut down.

This method is run before unregistering the component. The inputs & outputs are still wired and can be used, but other components might not be reacting to those events any more as they have already been shut down.

Return type:None
coroutine process(self)

Must be overridden to implement the component’s core logic.

It is never called before setup() or after cleanup() has been called for this component.

Return type:None
class BareComponent

Bases: async2v.components.base.Component

Unmanaged component

This component’s processing steps are not managed by the framework. Therefore, it cannot have any DoubleBufferedField fields, as those require the framework to know when the processing happens. The only builtin input field supported in this component is InputQueue.

Use this component for special cases like reading from external event sources or running all of the components logic in a separate thread.

coroutine setup(self)

Can be overridden to perform setup before processing starts.

This method is run after registration and app startup, i.e. all inputs & outputs are wired and can be used.

Return type:None
coroutine cleanup(self)

Can be overridden to perform cleanup before the component is shut down.

This method is run before unregistering the component. The inputs & outputs are still wired and can be used, but other components might not be reacting to those events any more as they have already been shut down.

Return type:None
class SubComponent

Bases: async2v.components.base._BaseComponent

Dependent component

A SubComponent is unmanaged with respect to processing, but it needs to be embedded into another component. It can have its own input & output fields. Processing needs to be triggered by the containing component. The containing component needs to extend from the ContainerMixin and register its supcomonents.

It is intended to support the composite pattern to design complex components.

Example usage:

>>> class SampleSubComponent(SubComponent):
>>>
>>>     def __init__(self):
>>>         self.input = Buffer('in', trigger=True)
>>>         self.output = Output('out')
>>>
>>>     def do_something(self):
>>>         for value in self.input.values:
>>>             self.output.push(value)
>>>
>>>
>>> class SampleComponent(EventDrivenComponent, ContainerMixin):
>>>
>>>     def __init__(self, sample: SampleSubComponent):
>>>         super().__init__([sample])
>>>
>>>     async def process(self):
>>>         for sub_component in self.sub_components:
>>>             sub_component.do_something()
class ContainerMixin(sub_components)

Bases: object

Mixin allowing components to have subcomponents.

See SubComponent for more information.

sub_components
Return type:List[SubComponent]
Returns:All registered subcomponents of this component