guidellm.utils.singleton
Singleton pattern implementations for ensuring single instance classes.
Provides singleton mixins for creating classes that maintain a single instance throughout the application lifecycle, with support for both basic and thread-safe implementations. These mixins integrate with the scheduler and other system components to ensure consistent state management and prevent duplicate resource allocation.
SingletonMixin
Basic singleton mixin ensuring single instance per class.
Implements the singleton pattern using class variables to control instance creation. Subclasses must call super().init() for proper initialization state management. Suitable for single-threaded environments or when external synchronization is provided.
Example: :: class ConfigManager(SingletonMixin): def init(self, config_path: str): super().init() if not self.initialized: self.config = load_config(config_path)
manager1 = ConfigManager("config.json")
manager2 = ConfigManager("config.json")
assert manager1 is manager2
Source code in src/guidellm/utils/singleton.py
initialized property
Return True if the singleton has been initialized.
__init__()
Initialize the singleton instance exactly once.
__new__(*args, **kwargs)
Create or return the singleton instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args | Positional arguments passed to the constructor | () | |
kwargs | Keyword arguments passed to the constructor | {} |
Returns:
| Type | Description |
|---|---|
| The singleton instance of the class |
Source code in src/guidellm/utils/singleton.py
ThreadSafeSingletonMixin
Bases: SingletonMixin
Thread-safe singleton mixin with locking mechanisms.
Extends SingletonMixin with thread safety using locks to prevent race conditions during instance creation in multi-threaded environments. Essential for scheduler components and other shared resources accessed concurrently.
Example: :: class SchedulerResource(ThreadSafeSingletonMixin): def init(self): super().init() if not self.initialized: self.resource_pool = initialize_resources()
Source code in src/guidellm/utils/singleton.py
thread_lock property
Return the thread lock for this singleton instance.
__init__()
Initialize the singleton instance with thread-safe initialization.
Source code in src/guidellm/utils/singleton.py
__new__(*args, **kwargs)
Create or return the singleton instance with thread safety.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args | Positional arguments passed to the constructor | () | |
kwargs | Keyword arguments passed to the constructor | {} |
Returns:
| Type | Description |
|---|---|
| The singleton instance of the class |
Source code in src/guidellm/utils/singleton.py
get_singleton_lock() classmethod
Get the class-specific singleton creation lock.