pynenc.runner.runner_context

Runner context for tracking execution environment details.

This module provides a simple, composable context class that can be nested to represent hierarchical execution environments (runner -> worker -> etc).

Key components:

  • RunnerContext: Single context class with optional parent reference

  • Automatic capture of pid, hostname, and thread_id

  • JSON serialization for cross-process communication

The context is designed to be simple and flexible - callers specify runner_cls and runner_id, and can create child contexts as needed.

Module Contents

Classes

RunnerContext

Context information for a runner execution environment.

API

class pynenc.runner.runner_context.RunnerContext[source]

Context information for a runner execution environment.

A simple, composable context that captures execution environment details. Can be nested via parent_ctx to represent hierarchical relationships (e.g., main runner -> worker process -> thread).

Parameters:
  • runner_cls (str) – The class name of the runner.

  • runner_id (str) – Identifier for this runner/context level.

  • parent_ctx (RunnerContext | None) – Optional parent context for hierarchy.

  • pid (int) – Process ID (auto-captured).

  • hostname (str) – Hostname (auto-captured).

  • thread_id (int) – Thread ID (auto-captured).

runner_cls: str

None

runner_id: str

‘field(…)’

parent_ctx: RunnerContext | None

None

pid: int

‘field(…)’

hostname: str

‘field(…)’

thread_id: int

‘field(…)’

classmethod from_runner(runner: pynenc.runner.base_runner.BaseRunner, parent_ctx: RunnerContext | None = None) pynenc.runner.runner_context.RunnerContext[source]

Create RunnerContext from a BaseRunner instance.

Parameters:
  • runner (BaseRunner) – The runner instance to extract context from.

  • parent_ctx (RunnerContext | None) – Optional parent context.

Returns:

A new RunnerContext instance populated with runner data.

property root_runner_id: str

Get the root runner_id by traversing up the parent chain.

Returns the runner_id of the topmost context in the hierarchy.

property root_runner_cls: str

Get the root runner_cls by traversing up the parent chain.

Returns the runner_cls of the topmost context in the hierarchy.

new_child_context(runner_cls: str, runner_id: str | None = None) pynenc.runner.runner_context.RunnerContext[source]

Create a child context with this context as parent.

Parameters:
  • runner_cls (str) – Class name for the child context.

  • runner_id (str) – Identifier for the child context.

Returns:

A new RunnerContext with this context as parent.

to_json() str[source]

Serialize the RunnerContext to a JSON string.

Recursively serializes the parent context if present.

Returns:

JSON representation of the context.

classmethod from_json(json_str: str) pynenc.runner.runner_context.RunnerContext[source]

Deserialize a RunnerContext from a JSON string.

Parameters:

json_str (str) – JSON string containing serialized context data.

Returns:

A new RunnerContext instance.

Raises:

ValueError – If the JSON data is invalid or missing required fields.

__repr__() str[source]

Return a concise string representation.