roboglia.base.BaseLoop

class BaseLoop(name='BASELOOP', patience=1.0, frequency=None, warning=0.9, throttle=0.1, review=1.0)[source]

Bases: roboglia.base.thread.BaseThread

This is a thread that executes in a separate thread, scheduling a certain atomic work (encapsulated in the atomic method) periodically as prescribed by the frequency parameter. The run method takes care of checking the flags for paused and stopped so there is no need to do this in the atomic method.

Parameters
  • name (str) – The name of the loop

  • patience (float) – A duration in seconds that the main thread will wait for the background thread to finish setup activities and indicate that it is in started mode.

  • frequency (float) – The loop frequency in [Hz]

  • warning (float) – Indicates a threshold in range [0..1] indicating when warnings should be logged to the logger in case the execution frequency is bellow the target. A 0.8 value indicates the real execution is less than 0.8 * target_frequency. The statistic is calculated over a period of time specified by the parameter review.

  • throttle (float) – Is a float (< 1.0) that is used by the monitoring of execution statistics to adjust the wait time in order to produce the desired processing frequency.

  • review (float) – The time in [s] to calculate the statistics for the frequency.

Raises
  • KeyError and ValueError if provided data in the initialization

  • dictionary are incorrect or missing.

__init__(name='BASELOOP', patience=1.0, frequency=None, warning=0.9, throttle=0.1, review=1.0)[source]

Initialize self. See help(type(self)) for accurate signature.

property frequency

Loop frequency.

property actual_frequency

Returns the actual running frequency that is calculated by statistics.

property period

Loop period = 1 / frequency.

property review

Indicates the amount of time in seconds before the thread will review the actual frequency against the target and take action.

property warning

Control the warning level for the warning message, the setter is smart: if the value is larger than 2 it will assume it is a percentage and divied it by 100 and ignore if the number is higher than 110. The over 100 is available for testing purposes.

property errors

Returns the number of errors logged by the statistics.

property processed

Returns the number of items processed in the current statistics. The items processed depends on the loop and might be different from the number of loops executed. For example the one execution loop might include several communication packets.

inc_errors()[source]

Used by subclasses to increment the number of errors.

inc_processed()[source]

Used by subclasses to increment the number of processed items.

property error_stat

(error in %, total errors, total items).

Type

Returns the error statistics as a tuple

run()[source]

Run method of the thread.

atomic()[source]

This method implements the periodic task that needs to be executed. It does not need to check paused or stopped as the run method does this already and the subclasses should make sure that the implementation completes quickly and does not raise any exceptions.

property name

Returns the name of the thread.

pause()

Requests the thread to pause.

property paused

Indicates the thread was paused.

resume()

Requests the thread to resume.

property running

Indicates if the thread is running.

setup()

Thread preparation before running. Subclasses should override

start(wait=True)

Starts the task in it’s own thread.

property started

Indicates if the thread was started.

stop(wait=True)

Sends the stopping signal to the thread. By default waits for the thread to finish.

property stopped

Indicates if the thread was stopped.

teardown()

Thread cleanup. Subclasses should override.