bgThread
Background service thread.
Attributes
| Attribute | Type | Description |
|---|---|---|
| __is_shutdown | threading.Event | A threading event used to signal the background loop to stop execution and begin a graceful shutdown. |
| __is_stopped | threading.Event | A threading event that is set when the thread has finished its execution loop or crashed, allowing other threads to synchronize on its termination. |
| daemon | boolean = True | A boolean flag indicating that the thread will run in the background and not prevent the main program from exiting. |
| name | string | The 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
| Name | Type | Description |
|---|---|---|
| name | string = None | The name of the thread. Defaults to the class name if not provided. |
| **kwargs | dict | Additional 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
| Name | Type | Description |
|---|---|---|
| msg | string | The format string used to construct the error message. |
| *fmt | any | Positional arguments used for string formatting in the error message. |
| **kwargs | any | Keyword 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.