Utils
Utility functions for singleton patterns, static variables, and hashing.
Utility functions for singleton patterns, static variables, and hashing.
SingletonMeta
Bases: type
Metaclass for singleton/multiton patterns with per-class locking.
Each class using this metaclass gets its own lock and instance store, so ClassA and ClassB do not contend with each other.
Attributes:
| Name | Type | Description |
|---|---|---|
_instance_lock |
Per-class threading lock. |
|
_instances |
Per-class instance dictionary keyed by arguments. |
Source code in shutils/utils.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | |
__call__(*args, **kwargs)
Create or return the cached instance using double-checked locking.
Source code in shutils/utils.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | |
singleton(cls=None, *, ignore_args=True)
Decorator that turns a class into a singleton.
Supports both @singleton (no parentheses) and
@singleton(ignore_args=False) (with parentheses).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
The class to decorate. Automatically provided when used without parentheses. |
None
|
|
ignore_args
|
If True, all calls return the same instance regardless of arguments. If False, instances are keyed by arguments. |
True
|
Returns:
| Type | Description |
|---|---|
|
The decorated class or a decorator function. |
Source code in shutils/utils.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | |
static_vars(**kwargs)
定义函数内静态变量的修饰器
Source code in shutils/utils.py
99 100 101 102 103 104 105 | |