dlt.common.configuration.container
Container Objects
class Container()
A singleton injection container holding several injection contexts. Implements basic dictionary interface.
Injection context is identified by its type and available via dict indexer. The common pattern is to instantiate default context value if it is not yet present in container.
By default, the context is thread-affine so it can be injected only n the thread that originally set it. This behavior may be changed in particular context type (spec).
The indexer is settable and allows to explicitly set the value. This is required by in any context that needs to be explicitly instantiated.
The injectable_context allows to set a context with a with keyword and then restore the previous one after it gets out of scope.
thread_contexts
Thread-id → (context dict, per-context RLock). Every thread gets its own lock.
injectable_context
@contextmanager
def injectable_context(
config: TInjectableContext,
lock_context_on_yield: bool = False) -> Iterator[TInjectableContext]
Insert config into the container and restore the previous value on exit.
Arguments:
configTInjectableContext - The context instance to inject.lock_context_on_yieldbool - When False (default), the per-context lock is released before yield and re-acquired for restore, allowing other threads to write to the same spec. When True, the lock is held for the entire lifetime including yield.
Yields:
TInjectableContext- The injectedconfiginstance.
Raises:
ContainerInjectableContextMangled- If another writer overwrote the context during yield (only possible whenlock_context_on_yieldis False).
get_worker_contexts
def get_worker_contexts(
) -> Dict[Type[ContainerInjectableContext], ContainerInjectableContext]
Collect all contexts marked with worker_affinity=True for the current thread.
Returns a dict mapping context type to instance for all contexts that should be passed to worker processes.
thread_pool_prefix
@staticmethod
def thread_pool_prefix() -> str
Creates a container friendly pool prefix that contains starting thread id. Container implementation will automatically use it for any thread-affine contexts instead of using id of the pool thread