bencher.variables.singleton_parametrized_sweep
Singleton variant of ParametrizedSweep (thread-safe).
Provides a per-subclass singleton with the smallest useful surface:
One instance per subclass via
__new__.Base
__init__calls the Parametrized chain exactly once.init_singleton()returns a result that is truthy on first call (backward-compatible withif self.init_singleton():) and also works as a context manager that auto-resets singleton state when thewithblock raises during first-time init.reset_singleton()classmethod to manually clear singleton state.All operations are thread-safe via an internal lock.
Example (boolean style — unchanged from before):
class MySweep(ParametrizedSweepSingleton):
def __init__(self, value=0):
if self.init_singleton():
self.value = value # only set once
super().__init__() # safe no-op after the first call
Example (context-manager style — auto-resets on failure):
class MySweep(ParametrizedSweepSingleton):
def __init__(self, **kwargs):
with self.init_singleton() as is_first:
if is_first:
self._do_fallible_setup(**kwargs)
super().__init__()
Classes
Ephemeral result from |
|
A minimal per-subclass singleton for ParametrizedSweep. |
Module Contents
- class bencher.variables.singleton_parametrized_sweep._SingletonInitResult(cls, is_first: bool)
Ephemeral result from
init_singleton().Bool —
bool(result)isTruewhen this is the first init.Context manager — on
__exit__, if the block raised and this was the first init, singleton bookkeeping is rolled back viareset_singleton()so a subsequent construction can retry.
- __slots__ = ('_cls', '_is_first')
- _cls
- _is_first
- __bool__() bool
- __enter__()
- __exit__(exc_type, exc_val, exc_tb)
- class bencher.variables.singleton_parametrized_sweep.ParametrizedSweepSingleton(**params)
Bases:
bencher.variables.parametrised_sweep.ParametrizedSweepA minimal per-subclass singleton for ParametrizedSweep.
Repeated construction returns the same instance for each subclass.
Ensures the Parametrized
__init__chain runs only once.init_singleton()returns a result that is truthy once per subclass and doubles as a context manager for automatic rollback on failure.reset_singleton()explicitly clears singleton state for a subclass.Thread-safe: all shared state is protected by
_lock.
- _instances
- _seen
- _lock
- _singleton_inited = True
- classmethod init_singleton() _SingletonInitResult
Mark cls as seen and return a
_SingletonInitResult.The result is truthy the first time a subclass calls this and falsy on every subsequent call — identical to the previous boolean return value.
It can also be used as a context manager:
with self.init_singleton() as is_first: if is_first: self._fallible_setup()
If the
withblock raises during a first-time init, the singleton bookkeeping is rolled back so the next construction can retry cleanly.
- classmethod reset_singleton() None
Clear singleton state for cls, allowing re-initialisation.