Skip to content

rhapsody.backends.execution.concurrent

Concurrent distributed execution backend for parallel and distributed computing.

This module provides a backend that executes tasks on local or single node HPC resources.

ConcurrentExecutionBackend

ConcurrentExecutionBackend(executor: Executor = None, name: str = 'concurrent')

Bases: BaseBackend

Simple async-only concurrent execution backend.

Attributes:

  • term_grace (float) –

    Seconds a cancelled command has to exit after SIGTERM, before SIGKILL.

  • kill_wait (float) –

    Seconds to wait for a command to finish after SIGKILL.

Source code in src/rhapsody/backends/execution/concurrent.py
def __init__(self, executor: Executor = None, name: str = "concurrent"):
    super().__init__(name=name)

    self.logger = _get_logger()

    if not executor:
        executor = ThreadPoolExecutor()
        self.logger.info("No executor was provided. Falling back to default ThreadPoolExecutor")

    if not isinstance(executor, Executor):
        err = "Executor must be ThreadPoolExecutor or ProcessPoolExecutor"
        raise TypeError(err)

    if isinstance(executor, ProcessPoolExecutor) and cloudpickle is None:
        raise ImportError(
            "ProcessPoolExecutor requires 'cloudpickle'. "
            "Install it with: pip install cloudpickle"
        )

    self.executor = executor
    # A process pool's tasks' functions, each pickled once while it stays the same.
    self._functions = FunctionPickles() if isinstance(executor, ProcessPoolExecutor) else None
    self.tasks: dict[str, asyncio.Task] = {}
    self._callback_func: Callable = lambda t, s: None
    self._initialized = False
    self._backend_state = BackendMainStates.INITIALIZED

name property

name: str

Name of the backend.

__await__

__await__()

Make backend awaitable.

Source code in src/rhapsody/backends/execution/concurrent.py
def __await__(self):
    """Make backend awaitable."""
    return self._async_init().__await__()

topology

topology()

Single node: local GPU count, cores capped at the executor's worker count.

Source code in src/rhapsody/backends/execution/concurrent.py
def topology(self):
    """Single node: local GPU count, cores capped at the executor's worker count."""
    import socket

    cores, gpus = self._probe_local_resources()
    max_workers = getattr(self.executor, "_max_workers", None)
    if max_workers:
        cores = min(cores, max_workers) if cores else max_workers
    return [{"id": socket.gethostname(), "cores": cores, "gpus": gpus}]

submit_tasks async

submit_tasks(tasks: list[dict[str, Any]]) -> list[Task]

Submit tasks for execution.

The backend keeps a task in tasks, and its handler in the task's "future", until the task ends.

Source code in src/rhapsody/backends/execution/concurrent.py
async def submit_tasks(self, tasks: list[dict[str, Any]]) -> list[asyncio.Task]:
    """Submit tasks for execution.

    The backend keeps a task in ``tasks``, and its handler in the task's ``"future"``, until
    the task ends.
    """
    # Set backend state to RUNNING when tasks are submitted
    if self._backend_state != BackendMainStates.RUNNING:
        self._backend_state = BackendMainStates.RUNNING
        self.logger.debug(f"Backend state set to: {self._backend_state.value}")

    submitted_tasks = []

    for task in tasks:
        future = asyncio.create_task(self._handle_task(task))
        submitted_tasks.append(future)
        if future.done():
            continue  # an eager task factory ran it to its end: there is nothing to keep

        self.tasks[task["uid"]] = task
        task["future"] = future

    return submitted_tasks

cancel_task async

cancel_task(uid: str) -> bool

Cancel a task by its UID.

Parameters:

  • uid (str) –

    The UID of the task to cancel.

Returns:

  • bool ( bool ) –

    True if the task was found and cancellation was attempted, False otherwise.

Source code in src/rhapsody/backends/execution/concurrent.py
async def cancel_task(self, uid: str) -> bool:
    """Cancel a task by its UID.

    Args:
        uid (str): The UID of the task to cancel.

    Returns:
        bool: True if the task was found and cancellation was attempted,
              False otherwise.
    """
    if uid in self.tasks:
        task = self.tasks[uid]
        future = task["future"]
        if future and future.cancelling():
            # The call that cancelled it reports it
            return not future.done()
        if future and future.cancel():
            # A cancelled command stops before the task ends, so CANCELED means that it
            # has stopped
            await asyncio.wait([future])
            # Set state on the task object itself before callback
            self._callback_func(task, "CANCELED")
            # A task cancelled before its handler started never ran the handler's release
            self._release(task, future)
            return True
    return False

cancel_all_tasks async

cancel_all_tasks() -> int

Cancel all running tasks, and wait for their commands to stop.

Source code in src/rhapsody/backends/execution/concurrent.py
async def cancel_all_tasks(self) -> int:
    """Cancel all running tasks, and wait for their commands to stop."""
    cancelled_count = 0
    cancelling = []
    for task in self.tasks.values():
        future = task["future"]
        if future.cancel():
            cancelling.append((task, future))
        cancelled_count += 1
    self.tasks.clear()
    if cancelling:
        await asyncio.wait([future for _, future in cancelling])
        for task, future in cancelling:
            self._release(task, future)
    return cancelled_count

shutdown async

shutdown() -> None

Shutdown the executor.

Source code in src/rhapsody/backends/execution/concurrent.py
async def shutdown(self) -> None:
    """Shutdown the executor."""
    # Set backend state to SHUTDOWN
    self._backend_state = BackendMainStates.SHUTDOWN
    self.logger.debug(f"Backend state set to: {self._backend_state.value}")

    await self.cancel_all_tasks()
    self.executor.shutdown(wait=True)
    if self._functions is not None:
        self._functions.clear()
    self.logger.info("Concurrent execution backend shutdown complete")

state async

state() -> str

Get backend state.

Returns:

  • str ( str ) –

    Current backend state (INITIALIZED, RUNNING, SHUTDOWN)

Source code in src/rhapsody/backends/execution/concurrent.py
async def state(self) -> str:
    """Get backend state.

    Returns:
        str: Current backend state (INITIALIZED, RUNNING, SHUTDOWN)
    """
    return self._backend_state.value

__aenter__ async

__aenter__()

Async context manager entry.

Source code in src/rhapsody/backends/execution/concurrent.py
async def __aenter__(self):
    """Async context manager entry."""
    if not self._initialized:
        await self._async_init()
    return self

__aexit__ async

__aexit__(exc_type, exc_val, exc_tb)

Async context manager exit.

Source code in src/rhapsody/backends/execution/concurrent.py
async def __aexit__(self, exc_type, exc_val, exc_tb):
    """Async context manager exit."""
    await self.shutdown()

create async classmethod

create(executor: Executor) -> ConcurrentExecutionBackend

Alternative factory method for creating initialized backend.

Parameters:

  • executor (Executor) –

    A concurrent.Executor instance (ThreadPoolExecutor or ProcessPoolExecutor).

Returns:

Source code in src/rhapsody/backends/execution/concurrent.py
@classmethod
async def create(cls, executor: Executor) -> "ConcurrentExecutionBackend":
    """Alternative factory method for creating initialized backend.

    Args:
        executor: A concurrent.Executor instance (ThreadPoolExecutor
                  or ProcessPoolExecutor).

    Returns:
        Fully initialized ConcurrentExecutionBackend instance.
    """
    backend = cls(executor)
    return await backend

register_callback

register_callback(func: Callable[[dict[str, Any], str], None]) -> None

Register a callback function for task state changes.

This chains the user's callback with the internal callback used by wait_tasks(). Both callbacks will be invoked on every state change.

Parameters:

  • func (Callable[[dict[str, Any], str], None]) –

    A callable that will be invoked when task states change. The function should accept task and state parameters.

Source code in src/rhapsody/backends/base.py
def register_callback(self, func: Callable[[dict[str, Any], str], None]) -> None:
    """Register a callback function for task state changes.

    This chains the user's callback with the internal callback used by wait_tasks().
    Both callbacks will be invoked on every state change.

    Args:
        func: A callable that will be invoked when task states change.
            The function should accept task and state parameters.
    """
    self._callback_func = func

register_emitter

register_emitter(emitter: Emitter) -> None

Receive the telemetry emitter, like :meth:register_callback receives the callback.

The :class:~rhapsody.telemetry.manager.TelemetryManager calls this when the backend is attached to a session or workflow engine that has telemetry (it is called again, with a new emitter, if the backend is attached to another manager). The default stores the emitter in self.telemetry, which the backend then uses to report plain data. Override it to react to the handoff and call super().register_emitter(emitter).

Parameters:

  • emitter (Emitter) –

    The handle with enabled and emit(...).

Source code in src/rhapsody/backends/base.py
def register_emitter(self, emitter: Emitter) -> None:
    """Receive the telemetry emitter, like :meth:`register_callback` receives the callback.

    The :class:`~rhapsody.telemetry.manager.TelemetryManager` calls this when the backend is
    attached to a session or workflow engine that has telemetry (it is called again, with a
    new emitter, if the backend is attached to another manager). The default stores the
    emitter in ``self.telemetry``, which the backend then uses to report plain data. Override
    it to react to the handoff and call ``super().register_emitter(emitter)``.

    Args:
        emitter: The handle with ``enabled`` and ``emit(...)``.
    """
    self.telemetry = emitter

is_available classmethod

is_available() -> bool

Return True if this backend's runtime dependencies are importable.

Used by :func:rhapsody.backends.discover_backends. A backend module imports even when its optional runtime (Dragon, Dask, Orbit, ...) is missing, so backends with optional dependencies override this to report whether they can actually be created.

Source code in src/rhapsody/backends/base.py
@classmethod
def is_available(cls) -> bool:
    """Return True if this backend's runtime dependencies are importable.

    Used by :func:`rhapsody.backends.discover_backends`. A backend module imports even when
    its optional runtime (Dragon, Dask, Orbit, ...) is missing, so backends with
    optional dependencies override this to report whether they can actually be created.
    """
    return True