Skip to main content

bgThread

Background service thread.

Attributes

AttributeTypeDescription
__is_shutdownthreading.EventA threading event used to signal the background loop to stop execution and begin a graceful shutdown.
__is_stoppedthreading.EventA threading event that is set when the thread has finished its execution loop or crashed, allowing other threads to synchronize on its termination.
daemonboolean = TrueA boolean flag indicating that the thread will run in the background and not prevent the main program from exiting.
namestringThe display name of the thread, defaulting to the class name if no specific name is provided during initialization.

Constructor

Signature

def bgThread(
name: string = None,
**kwargs: dict
) - > null

Parameters

NameTypeDescription
namestring = NoneThe name of the thread. Defaults to the class name if not provided.
**kwargsdictAdditional keyword arguments passed to the thread constructor.

Methods


body()

@classmethod
def body()

Defines the core logic to be executed repeatedly in the background loop. This method must be overridden by subclasses or it will raise a NotImplementedError.


on_crash()

@classmethod
def on_crash(
msg: string,
*fmt: any,
**kwargs: any
)

Handles exceptions by printing a formatted error message and the stack trace to stderr. This method is triggered automatically if the body method raises an unhandled exception.

Parameters

NameTypeDescription
msgstringThe format string used to construct the error message.
*fmtanyPositional arguments used for string formatting in the error message.
**kwargsanyKeyword arguments used for string formatting in the error message.

run()

@classmethod
def run()

Executes the background loop, repeatedly calling the body method until a shutdown is signaled. If an exception occurs, it triggers the crash handler and forces a process exit.


stop()

@classmethod
def stop()

Graceful shutdown.