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.

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]

View source on GitHub

Insert config into the container and restore the previous value on exit.

Arguments:

  • config TInjectableContext - The context instance to inject.
  • lock_context_on_yield bool - 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 another writer overwrote the context during yield (only possible when lock_context_on_yield is False).

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 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

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.