Skip to main content

Make python code durable

Project description

durable-python

Make Python async functions durable and resumable with a minimal, pluggable runtime.

Overview

durable-python

This package treats durability as a core runtime concern. Async functions are transformed into a sequence of CodeBlocks that can be checkpointed, paused, and resumed through a DurableRuntime. Everything is open and pluggable: orchestration backends implement a small interface, and persistence is provided through StateStore implementations.

Key primitives:

  • DurableRuntime — drives execution and persistence.
  • CallStack / FunctionCall / CodeBlock — durable execution model.
  • OrchestrationBackend / DurableBackend — orchestration + event waiting.
  • StateStore / InMemoryStateStore / DiskStateStore — persistence.
  • DurableAstTransformer / make_durable — transform async functions into durable programs.

Quick Start

import asyncio
from durable import DurableBackend, InMemoryStateStore, PauseForEventException, make_durable

# Define your async function
async def greet(name: str):
    print("waiting for permission to greet")
    raise PauseForEventException("allow")
    return f"hello {name}"

# Make it durable
durable_greet = make_durable(greet)

async def main():
    backend = DurableBackend(InMemoryStateStore())

    async def run_once():
        # Triggers runtime execution; will pause on PauseForEventException
        try:
            return await durable_greet("Ada", orchestration_backend=backend, instance_id="greet-1")
        except PauseForEventException as e:
            print(f"paused for event {e.event}")

    await run_once()                 # pauses and persists state
    backend.publish_event("greet-1", "allow")  # deliver event
    result = await durable_greet("Ada", orchestration_backend=backend, instance_id="greet-1")
    print(result)                    # "hello Ada"

asyncio.run(main())

Architecture

  • Execution model: FunctionCall holds locals, an ordered list of CodeBlocks, and the current index. CallStack orchestrates nested calls and return propagation.
  • Durable runtime: DurableRuntime restores saved state (if any), drives the call stack, and delegates persistence plus event waiting to an OrchestrationBackend.
  • Backends: OrchestrationBackend defines load_state, save_state, and wait_for_event. DurableBackend is the OSS implementation backed by a StateStore and an in-memory event bus.
  • Persistence: StateStore defines load / save. The OSS package ships InMemoryStateStore and DiskStateStore(path).
  • Transformation: DurableAstTransformer rewrites async functions into checkpointable code. make_durable(fn) wraps a function into a DurableProgram that builds a runtime and executes it.

Custom Backends

Implement OrchestrationBackend to integrate with your own orchestration layer:

from durable import OrchestrationBackend

class CustomBackend(OrchestrationBackend):
    async def load_state(self, instance_id: str) -> dict | None:
        ...

    async def save_state(self, instance_id: str, state: dict) -> None:
        ...

    async def wait_for_event(self, instance_id: str, event_name: str) -> None:
        ...

Use it by passing orchestration_backend= to the callable returned by make_durable.

Testing

Install dev dependencies and run pytest:

poetry install --with dev
poetry run pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

durable_python-0.1.0.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

durable_python-0.1.0-py3-none-any.whl (15.5 kB view details)

Uploaded Python 3

File details

Details for the file durable_python-0.1.0.tar.gz.

File metadata

  • Download URL: durable_python-0.1.0.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.4 CPython/3.13.5 Darwin/24.3.0

File hashes

Hashes for durable_python-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c4b487b220446351b7b50a26b9945010bb6b00e25662761ccac1c9d64c04002a
MD5 a3f4b8cc56993c99679dfafca15178fb
BLAKE2b-256 1fb347ed8bb8c3515fe9c523f7f5facbd7e115c84ba5a14bf9df868d285904d3

See more details on using hashes here.

File details

Details for the file durable_python-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: durable_python-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 15.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.4 CPython/3.13.5 Darwin/24.3.0

File hashes

Hashes for durable_python-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8f7a0075935068a390a1b0a9898a793c7457b9d1d297beb942d8f3d495fbd251
MD5 99b02d82bb3e32fd7124428c5448aad2
BLAKE2b-256 480665f5d6df9c3ae48c8b15683db01e19433248053935a737ce0473e8300606

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page