pynenc.context

Context management for Pynenc execution environments.

This module maintains context for invocations and execution within the Pynenc application. It provides thread-local storage for runner contexts that can be hierarchically nested.

Key components:

  • RunnerContext management (set/get/clear)

  • Distributed invocation context tracking

  • Automatic logging context integration

Module Contents

Functions

_get_sync_inv_context_storage

Get thread-local sync invocation context storage, creating if needed.

_get_runner_context_storage

Get thread-local runner context storage, creating if needed.

_get_dist_inv_context_storage

Get thread-local distributed invocation context storage, creating if needed.

_get_app_storage

Get thread-local app storage.

get_runner_context

Get the current runner context for the given app.

set_runner_context

Set the runner context for the given app.

clear_runner_context

Clear the runner context for the given app.

get_or_create_runner_context

Get the current runner context, creating an ExternalRunner context if none exists.

get_dist_invocation_context

Get the current distributed invocation context for the given app.

swap_dist_invocation_context

Set the current invocation context for the given app and returns the previous.

get_current_app

Get the current app from thread-local storage.

set_current_app

Set the current app in thread-local storage.

get_runner_args

Get the runner arguments from thread-local storage.

set_runner_args

Set the runner arguments in thread-local storage.

_get_runner_storage

Get thread-local runner storage, creating if needed.

get_current_runner

Retrieve the current runner for the given app_id from thread-local storage.

set_current_runner

Set the current runner for the given app_id in thread-local storage.

clear_current_runner

Clear the current runner and all associated contexts for the given app_id.

Data

API

pynenc.context.thread_local

‘local(…)’

pynenc.context._get_sync_inv_context_storage() dict[str, ConcurrentInvocation | None][source]

Get thread-local sync invocation context storage, creating if needed.

pynenc.context._get_runner_context_storage() dict[str, pynenc.runner.runner_context.RunnerContext][source]

Get thread-local runner context storage, creating if needed.

pynenc.context._get_dist_inv_context_storage() dict[str, DistributedInvocation | None][source]

Get thread-local distributed invocation context storage, creating if needed.

pynenc.context._get_app_storage() Pynenc | None[source]

Get thread-local app storage.

pynenc.context.get_runner_context(app_id: str) pynenc.runner.runner_context.RunnerContext | None[source]

Get the current runner context for the given app.

Parameters:

app_id (str) – The app identifier.

Returns:

The current runner context, or None if not set.

pynenc.context.set_runner_context(app_id: str, runner_ctx: pynenc.runner.runner_context.RunnerContext) None[source]

Set the runner context for the given app.

Parameters:
  • app_id (str) – The app identifier.

  • runner_ctx (RunnerContext) – The runner context to set.

pynenc.context.clear_runner_context(app_id: str) None[source]

Clear the runner context for the given app.

Parameters:

app_id (str) – The app identifier.

pynenc.context.get_or_create_runner_context(app_id: str) pynenc.runner.runner_context.RunnerContext[source]

Get the current runner context, creating an ExternalRunner context if none exists.

Parameters:

app_id (str) – The app identifier.

Returns:

The current runner context (never None).

pynenc.context.get_current_runner_context

None

pynenc.context.get_dist_invocation_context(app_id: str) DistributedInvocation | None[source]

Get the current distributed invocation context for the given app.

Parameters:

app_id (str) – The app identifier.

Returns:

The current invocation context for the given app.

pynenc.context.swap_dist_invocation_context(app_id: str, invocation: DistributedInvocation | None) DistributedInvocation | None[source]

Set the current invocation context for the given app and returns the previous.

Parameters:
  • app_id (str) – The app identifier.

  • invocation (DistributedInvocation) – The invocation to set as the current context.

Returns:

The previous invocation context.

pynenc.context.get_current_app() Pynenc | None[source]

Get the current app from thread-local storage.

Returns:

The current app instance, or None if not set.

pynenc.context.set_current_app(app: pynenc.app.Pynenc) None[source]

Set the current app in thread-local storage.

Parameters:

app (Pynenc) – The app instance to set.

pynenc.context.get_runner_args() dict[str, Any] | None[source]

Get the runner arguments from thread-local storage.

Returns:

The runner arguments for the current thread, or None if not set.

pynenc.context.set_runner_args(args: dict[str, Any] | None) None[source]

Set the runner arguments in thread-local storage.

Parameters:

args (dict[str, Any] | None) – The runner arguments to set.

pynenc.context._get_runner_storage() dict[str, pynenc.runner.base_runner.BaseRunner][source]

Get thread-local runner storage, creating if needed.

pynenc.context.get_current_runner(app_id: str) BaseRunner | None[source]

Retrieve the current runner for the given app_id from thread-local storage.

This function allows each thread or process to access its own runner instance, which is critical in multi-process environments like MultiThreadRunner where each process runs a ThreadRunner and needs to reference its own runner instance without conflicting with others.

Parameters:

app_id (str) – The application identifier.

Returns:

The current runner instance if set in the current thread/process, else None.

pynenc.context.set_current_runner(app_id: str, runner: pynenc.runner.base_runner.BaseRunner) None[source]

Set the current runner for the given app_id in thread-local storage.

This is used to associate a runner with the current thread or process context, enabling isolated execution environments.

Also creates and sets the runner context from the runner.

Parameters:
  • app_id (str) – The application identifier.

  • runner (BaseRunner) – The runner instance to set.

pynenc.context.clear_current_runner(app_id: str) None[source]

Clear the current runner and all associated contexts for the given app_id.

Also clears the logging context.

Parameters:

app_id (str) – The application identifier.