Helper
Module for various helper classes and functions.
This module provides utility functions and classes that assist with system operations such as checking time synchronization and handling graceful termination of processes. The functionality is tailored to Unix-based systems.
Usage
The primary components of this module are:
- check_time_sync
: A function that checks whether the system's clock is synchronized.
- GracefulKiller
: A class that allows for graceful termination of a running application
upon receiving interrupt signals (e.g., SIGINT, SIGTERM).
Examples:
Using GracefulKiller
to handle termination signals:
>>> killer = GracefulKiller()
>>> while not killer.kill_now:
>>> # Perform long-running task
>>> pass
>>> print("Application terminated gracefully.")
Classes:
Name | Description |
---|---|
GracefulKiller |
Handles system signals for graceful application termination. |
Functions:
Name | Description |
---|---|
check_time_sync |
Checks if the system clock and RTC are synchronized. |
Notes
- This module is intended for use on Unix-based systems.
GracefulKiller
Handles system signals for graceful application termination.
GracefulKiller
listens for interrupt signals (e.g., SIGINT, SIGTERM) and sets a flag
when such a signal is received, allowing the application to terminate gracefully.
Attributes:
Name | Type | Description |
---|---|---|
kill_now |
bool
|
A flag indicating whether a termination signal has been received. |
Methods:
Name | Description |
---|---|
exit_gracefully |
Signal handler that sets the |
Examples:
>>> killer = GracefulKiller()
>>> while not killer.kill_now:
>>> # Perform long-running task
>>> pass
>>> print("Application terminated gracefully.")
__init__()
Initialize the GracefulKiller instance and set up signal handlers.
Registers the signal handlers for SIGINT and SIGTERM, which trigger the
exit_gracefully
method to handle termination signals.
exit_gracefully(signum, frame)
Handle termination signals and set the kill flag.
This method is called when a registered signal (SIGINT or SIGTERM) is received.
It sets the kill_now
attribute to True
, indicating that the application
should terminate.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
signum |
int
|
The signal number received. |
required |
frame |
any
|
The current stack frame (unused in this method). |
required |
Notes
This method is intended to be used as a signal handler and is not typically called directly.
check_time_sync(sync_status)
Check if the system clock and RTC are synchronized.
This function uses the timedatectl
command to check whether the system clock is
synchronized and if the RTC (Real-Time Clock) is set. The results are stored in
the provided sync_status
list.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sync_status |
list
|
A list to store the synchronization status. The first element
is set to |
required |
Examples: