Skip to content

senxor.thread

senxor.thread

Thread utilities for Senxor devices.

CVCamThread

A threaded wrapper for OpenCV's VideoCapture for non-blocking reads with a listener pattern.

This class continuously reads frames from a camera in a background thread. It implements a "consume-on-read" pattern for its read() method and provides a listener interface for push-based notifications.

__init__(capture, *, allow_listener=True)

Initialize the CVCamThread.

Parameters:

Name Type Description Default
capture VideoCapture

A pre-configured OpenCV VideoCapture instance.

required
allow_listener bool

Whether to enable the listener pattern.

True

add_listener(fn, name=None)

Register a listener called with (success, frame) tuple.

Parameters:

Name Type Description Default
fn Callable[[bool, ndarray], None]

Callable invoked with the latest frame. Must be lightweight and non-blocking.

required
name str | None

Optional unique identifier. If omitted, an automatic listener_X name is assigned.

None

Returns:

Type Description
str

The listener name actually registered.

Notes

Listener functions must be extremely lightweight and non-blocking. If a listener function takes too long to execute, the _BackgroundReader will raise a TimeoutError in the reading thread when the next frame arrives. This strict policy ensures that listener notifications do not fall behind the camera's frame rate.

read()

Return the newest frame and consume it.

Returns:

Type Description
tuple[bool, ndarray] or tuple[False, None]

A tuple containing a boolean indicating success and the frame data, or (False, None) if no new frame is available.

Raises:

Type Description
RuntimeError

If the thread has not been started.

remove_listener(name)

Remove a previously registered listener by name.

start()

Start background processing (idempotent).

stop()

Stop background processing (idempotent).

SenxorThread

A threaded wrapper for Senxor for non-blocking reads with a listener pattern.

This class continuously reads data from a Senxor device in a background thread. It implements a "consume-on-read" pattern for its read() method and provides a listener interface for push-based notifications.

__init__(senxor, *, frame_unit='C', allow_listener=True)

Initialize the SenxorThread.

Parameters:

Name Type Description Default
senxor Senxor

A Senxor instance.

required
frame_unit ('C', 'dK')

The unit of the frame data. "C" for Celsius (float32), "dK" for deci-Kelvin (uint16).

"C"
allow_listener bool

Whether to enable the listener pattern. Defaults to True.

True

add_listener(fn, name=None)

Register a listener called with (header, frame) tuple.

The supplied fn must accept two positional arguments (header, frame). Internally the call is adapted to the generic listener signature.

Parameters:

Name Type Description Default
fn Callable[[ndarray, ndarray], None]

Callable invoked with the latest data object. Must be lightweight and non-blocking.

required
name str | None

Optional unique identifier. If omitted, an automatic listener_X name is assigned.

None
Notes
Listener functions **must** be extremely lightweight and non-blocking.
If a listener function takes too long to execute, the `BackgroundReader`
will raise a `TimeoutError` in the reading thread when the
next frame arrives. This strict policy ensures that listener notifications
do not fall behind the sensor's frame rate.

read()

Return the newest (header, frame) pair and consume it.

Raises:

Type Description
RuntimeError

If the thread has not been started.

remove_listener(name)

Remove a previously registered listener by name.

start()

Connect to device and start background processing (idempotent).

stop()

Stop background processing and close device (idempotent).