Skip to main content
Version: devel

dlt.common.configuration.container

Container Objects​

class Container()

View source on GitHub

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"

View source on GitHub

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]

View source on GitHub

Insert config into the current execution context and restore the previous value on exit.

Arguments:

  • config TInjectableContext - The context instance to inject.
  • lock_context_on_yield bool - Applies to contexts with global_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 injected config instance.

Raises:

  • ContainerInjectableContextMangled - If the context was replaced during yield.

get_worker_contexts​

def get_worker_contexts(
) -> Dict[Type[ContainerInjectableContext], ContainerInjectableContext]

View source on GitHub

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

View source on GitHub

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.

This demo works on codespaces. Codespaces is a development environment available for free to anyone with a Github account. You'll be asked to fork the demo repository and from there the README guides you with further steps.
The demo uses the Continue VSCode extension.

Off to codespaces!

DHelp

Ask a question

Welcome to "Codex Central", your next-gen help center, driven by OpenAI's GPT-4 model. It's more than just a forum or a FAQ hub – it's a dynamic knowledge base where coders can find AI-assisted solutions to their pressing problems. With GPT-4's powerful comprehension and predictive abilities, Codex Central provides instantaneous issue resolution, insightful debugging, and personalized guidance. Get your code running smoothly with the unparalleled support at Codex Central - coding help reimagined with AI prowess.