async2v.fields module¶
Summary¶
Classes:
AveragingOutput |
Averaging output field |
Buffer |
Provides all events received since the last processing step |
DoubleBufferedField |
Abstract base class for most input fields |
History |
Provides the last maxlen events |
InputField |
Abstract base class for all input fields |
InputQueue |
Unmanaged input field appending incoming events to a queue |
Latest |
Provides the latest received event to the component |
LatestBy |
Provides the latest received event to the component by category |
Output |
All-purpose output field |
Reference¶
-
class
InputField
(key)¶ Bases:
typing.Generic
Abstract base class for all input fields
-
set
(new)¶ Push new event into the field.
This method is used by the framework to push events to components. You should not need to call this method from your production code.
Return type: None
-
-
class
DoubleBufferedField
(key, trigger=False)¶ Bases:
async2v.fields.InputField
,typing.Generic
Abstract base class for most input fields
Implements double buffering to guarantee components stable content of their input fields during one processing step, even if new events are arriving meanwhile.
Parameters: - key (
str
) – Address that connects output to input fields - trigger (
bool
) – Set toTrue
to trigger processing step when a newEvent
arrives. Only allowed within aEventDrivenComponent
.
-
switch
()¶ Switch the double buffer.
This method is used by the framework to make new values available for the next processing step. You should not need to call this method from your production code.
Return type: None
- key (
-
class
Latest
(key, trigger=False)¶ Bases:
async2v.fields.DoubleBufferedField
,typing.Generic
Provides the latest received event to the component
Parameters: - key (
str
) – Address that connects output to input fields - trigger (
bool
) – Set toTrue
to trigger processing step when a newEvent
arrives. Only allowed within aEventDrivenComponent
.
- key (
-
class
LatestBy
(key, classifier, trigger=False)¶ Bases:
async2v.fields.DoubleBufferedField
Provides the latest received event to the component by category
Incoming events are assigned to categories by the given
classifier
function. For each event class, the latest event is retained (the same way as in theLatest
field). The latest events per category can be accessed asdict
.- Example storing the latest message of a specific length:
>>> message_by_length = LatestBy[str, int]('message', lambda m: len(m)) >>> # The following 4 lines simulate the framework delivering events >>> message_by_length.set(Event[str]('message', 'Hello')) >>> message_by_length.set(Event[str]('message', 'World')) >>> message_by_length.set(Event[str]('message', 'Hello World!')) >>> message_by_length.switch() >>> # The latest values are available by category >>> message_by_length.value_dict {5: 'World', 12: 'Hello World!'}
Parameters: -
events
¶ Latest received events per category
Empty
dict
if no event has been received.Return type: Dict
[~K,Event
[~T]]
-
class
Buffer
(key, maxlen=None, trigger=False)¶ Bases:
async2v.fields.DoubleBufferedField
,typing.Generic
Provides all events received since the last processing step
In the next processing step, the buffer contains only events received after the ones from the previous step.
Parameters: - key (
str
) – Address that connects output to input fields - trigger (
bool
) – Set toTrue
to trigger processing step when a newEvent
arrives. Only allowed within aEventDrivenComponent
.
-
events
¶ All events received since the last processing step in received order.
Empty list if no event has been received.
Return type: List
[Event
[~T]]
- key (
-
class
History
(key, maxlen, trigger=False)¶ Bases:
async2v.fields.DoubleBufferedField
,typing.Generic
Provides the last
maxlen
eventsOther than the
Buffer
field, aHistory
field may provide events that have already been processed again, as the events are not cleared between processing steps. It works like aLatest
field that can store more than one historic value.Parameters: -
events
¶ The last
maxlen
events in received order.Empty list if no event has been received.
Return type: List
[Event
[~T]]
-
-
class
InputQueue
(key, maxlen=None)¶ Bases:
async2v.fields.InputField
,typing.Generic
Unmanaged input field appending incoming events to a queue
This input field is useful for components that don’t rely on the framework to trigger processing steps (subclasses of
BareComponent
). Internally, adeque
is used, which is safe for threading scenarios (as long asjyou usepopleft()
to read events).
-
class
Output
(key)¶ Bases:
typing.Generic
All-purpose output field
Push events here during processing. They will be routed to all relevant input fields (matched by key).
Parameters: key ( str
) – Address that connects output to input fields-
set_queue
(queue_)¶ This method is used by the framework to inject the central application queue. You should not need to call this method from your production code.
-
-
class
AveragingOutput
(key, count=None, interval=None)¶ Bases:
async2v.fields.Output
,typing.Generic
Averaging output field
Push the average of the values collected every count values or after the given interval, whichever happens first. The value type
T
needs to support__add__(T, T)
and__truediv__(T, int)
to compute a meaningful average.An event can only be emitted during a call to
push
. If the interval is expired but no further values are pushed, no output is generated.This field is intended for special use cases like metrics. For example, it is used by the framework to report performance indicators (fps, processing time).
Parameters: -
push
(value, timestamp=None)¶ Push a new output value
Depending on the configuration if this field, not all pushed values cause new events to be emitted.
Parameters:
-