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.
The indexer is settable and allows to explicitly set the value. A context set this way is thread-affine: it is visible in the
thread that set it and in the dlt pools started by that thread (see thread_pool_prefix). Specs with global_affinity are
visible in all threads.
The injectable_context sets a context with a with keyword and restores the previous one after it gets out of scope. The
injected context belongs to the current execution context (contextvars). Work submitted to dlt thread pools and asyncio
tasks starts from a copy of it, so they see the contexts injected by the submitter and keep their own injections private.
Specs with global_affinity are injected into the main thread.
thread_contexts
Thread-id → (context dict, per-context RLock). Every thread gets its own lock.
reset_main_thread
@classmethod
def reset_main_thread(cls) -> "Container"
Discards the singleton and re-captures the main thread id, returning a clean container.
injectable_context
@contextmanager
def injectable_context(
config: TInjectableContext,
lock_context_on_yield: bool = False) -> Iterator[TInjectableContext]
Insert config into the current execution context and restore the previous value on exit.
Arguments:
configTInjectableContext - The context instance to inject.lock_context_on_yieldbool - Applies to contexts withglobal_affinity. 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 the context was replaced during yield.
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 thread name prefix for dlt pools. Pool threads see the contexts set in the thread
that started the outermost pool. Injected contexts reach them only through a copied contextvars
context, see TimeoutThreadPoolExecutor.submit.