Orchestration

Multi-study orchestration engine for iMednet SDK.

Provides MultiStudyOrchestrator for executing pipeline functions concurrently across multiple iMednet study boundaries with fault isolation, telemetry context propagation, and normalized result reporting.

Quick start

from typing import Any
from imednet import ImednetSDK, MultiStudyOrchestrator

def my_pipeline(
    study_key: str,
    sdk_client: Any,
    logger: Any,
    *args: Any,
    **kwargs: Any,
) -> dict[str, int]:
    logger.info("Processing study")
    subjects = sdk_client.subjects.list(study_key=study_key)
    return {"subject_count": len(list(subjects))}

with ImednetSDK() as sdk:
    orchestrator = MultiStudyOrchestrator(sdk, max_workers=4)
    results = orchestrator.execute_pipeline(my_pipeline)

for study_key, result in results.items():
    if result["status"] == "SUCCESS":
        print(f"{study_key}: {result['data']}")
    else:
        print(f"{study_key}: FAILED — {result['error']}")

Public API

  • MultiStudyOrchestrator — main orchestration engine

  • StudyContextLogAdapter — per-study log adapter

  • make_study_logger() — factory for study-bound loggers

  • OrchestratorResult — per-study result schema (TypedDict)

  • StudyWorkerCallable — protocol for pipeline callables

Errors

class imednet.orchestration.MultiStudyOrchestrator[source]

Bases: object

Synchronous multi-study orchestrator wrapping AsyncMultiStudyOrchestrator.

Strictly typed against ImednetFacade while sharing the canonical async execution, study filtering, context isolation, and adaptive backpressure engine. Safely executes even when called from an active event loop in Jupyter or Pyodide.

__init__(sdk, max_workers=4)[source]

Initialize the multi-study orchestrator.

Parameters:
  • sdk (ImednetFacade) – An initialized synchronous ImednetSDK or ImednetFacade.

  • max_workers (int) – Maximum number of concurrent worker threads. Defaults to 4.

Raises:
  • ValueError – If max_workers < 1.

  • TypeError – If an AsyncImednetSDK or async facade is provided.

Return type:

None

execute_pipeline(pipeline_func, whitelist=None, blacklist=None, *args, **kwargs)[source]

Execute a synchronous pipeline function concurrently across resolved studies.

Parameters:
  • pipeline_func (StudyWorkerCallable[T_Output] | Callable[..., T_Output]) –

  • whitelist (set[str] | None) –

  • blacklist (set[str] | None) –

  • args (Any) –

  • kwargs (Any) –

Return type:

dict[str, OrchestratorResult]

property max_workers: int

Maximum number of concurrent worker threads.

resolve_active_studies(whitelist=None, blacklist=None)[source]

Query the iMednet registry and apply whitelist/blacklist filtering rules.

Parameters:
  • whitelist (set[str] | None) –

  • blacklist (set[str] | None) –

Return type:

list[str]

property sdk: ImednetFacade

The shared read-only synchronous SDK facade.

class imednet.orchestration.StudyContextLogAdapter[source]

Bases: LoggerAdapter

A logger adapter that enriches records with a bound study key.

__init__(logger, study_key)[source]

Initialize the study context log adapter.

Parameters:
  • logger (Logger) – The underlying logger to wrap.

  • study_key (str) – The study identifier to inject into every log record.

Return type:

None

process(msg, kwargs)[source]

Inject study_key and studyKey into the log record extra mapping.

Parameters:
  • msg (Any) –

  • kwargs (MutableMapping[str, Any]) –

Return type:

tuple[Any, MutableMapping[str, Any]]

property study_key: str

The study identifier bound to this adapter.

imednet.orchestration.OrchestratorResult = <class 'imednet.orchestration.types.OrchestratorResult'>[source]

Normalized result entry returned per study by execute_pipeline.

imednet.orchestration.StudyWorkerCallable = <class 'imednet.orchestration.types.StudyWorkerCallable'>[source]

Defines the explicit signature required for injected synchronous pipeline tasks.

Pipeline callables injected into MultiStudyOrchestrator.execute_pipeline must conform to this protocol.

Orchestration Errors

exception imednet.errors.OrchestratorError[source]

Bases: ImednetError

Base exception for all orchestration-layer failures.

Raised when the MultiStudyOrchestrator encounters a structural or configuration error that prevents pipeline execution from starting.

Individual per-study runtime failures are NOT raised as exceptions — they are captured in the OrchestratorResult result matrix with status="FAILED".

__init__(message='', status_code=None, code=None, status=None, *, diagnostic_context=None)

Initialize the error.

Parameters:
  • message (str) –

  • status_code (int | None) –

  • code (int | None) –

  • status (Any) –

  • diagnostic_context (DiagnosticContext | None) –

Return type:

None

property attempt_count: int | None

Return the number of attempts executed before failure, if present.

property cause_type: str | None

Return the underlying exception class name, if present.

property method: str | None

Return the HTTP method from diagnostic_context, if present.

property retry_after: float | None

Return the parsed Retry-After delay in seconds, if present.

property url: str | None

Return the sanitized request URL from diagnostic_context, if present.

exception imednet.errors.FilterConflictError[source]

Bases: OrchestratorError

Raised when both whitelist and blacklist are non-empty simultaneously.

The whitelist and blacklist filters are mutually exclusive operations. Providing both simultaneously creates ambiguous behavior and is rejected at validation time before any study resolution occurs.

Example:

# This raises FilterConflictError — a study key cannot be in both:
orchestrator.execute_pipeline(
    my_func,
    whitelist={"STUDY-A", "STUDY-B"},
    blacklist={"STUDY-C"},
)
__init__(whitelist, blacklist)[source]

Initialize a filter conflict error.

Parameters:
  • whitelist (set[str]) – The provided study key whitelist.

  • blacklist (set[str]) – The provided study key blacklist.

Return type:

None

property attempt_count: int | None

Return the number of attempts executed before failure, if present.

property cause_type: str | None

Return the underlying exception class name, if present.

property method: str | None

Return the HTTP method from diagnostic_context, if present.

property retry_after: float | None

Return the parsed Retry-After delay in seconds, if present.

property url: str | None

Return the sanitized request URL from diagnostic_context, if present.