Skip to main content

Brokerless durable execution for Python.

Project description

Substantial

PyPI version

Brokerless durable execution for Python.

Substantial is part of the Metatype ecosystem. Consider checking out how this component integrates with the whole ecosystem and browse the documentation to see more examples.

What is durable execution?

Durable execution is a programming model where the state of a function is preserved across failures, restarts, or other (un)voluntary disruptions. This ensures that applications can continue execution from their last stable state without losing context or causing additional side effects. It is particularly well-suited for long-running workflows as it enables the management of complex sequences of steps, handling breaks, retries and recovery gracefully.

Substantial is designed around a replay mechanism that reconstructs the function state by re-executing historical events from stored logs. All the logic is embedded in a protocol and there is no centralized broker, allowing it to work with any backend (local files, cloud storage and databases). It aims to be an alternative for use cases that do not require the scale and complexity of Temporal or Durable Task.

Getting started

# pypi
pip install substantial
poetry add substantial

# remote master
pip install --upgrade git+https://github.com/zifeo/substantial.git
poetry add git+https://github.com/zifeo/substantial.git

# local repo/dev
poetry install
pre-commit install
protoc -I . --python_betterproto_out=. protocol/*

Workflow

from dataclasses import dataclass
from datetime import timedelta
import random
from substantial import workflow, Context


def roulette():
    if random.random() < 1 / 6:
        raise Exception("shot!")
    return 100


def multiply(a, b):
    return a * b


@dataclass
class State:
    cancelled: bool = False

    def update(self):
        self.cancelled = True


@workflow()
async def example(c: Context):
    res = await c.save(roulette)

    await c.sleep(timedelta(seconds=10))
    res = await c.save(lambda: multiply(res, 0.5))

    n = await c.receive("by")
    res = await c.save(lambda: multiply(res, n))

    s = State(cancelled=False)
    c.handle("cancel", lambda _: s.update())

    await c.ensure(lambda: s.cancelled)
    return res

Worker

Brokerless means that there is no active broker and all the scheduling is solely organized around files/key values. Substantial currently supports Redis, local files and s3-compatible object storages.

import asyncio
from substantial import Conductor
from substantial.backends.fs import FSBackend
from demo.readme_ws import example


async def main():
    backend = FSBackend("./demo/logs/readme")
    substantial = Conductor(backend)
    substantial.register(example)

    # run the agent in the background
    agent = substantial.run()

    # start the workflow
    w = await substantial.start(example)

    await asyncio.sleep(3)
    print(await w.send("by", 2))

    await asyncio.sleep(5)
    print(await w.send("cancel"))

    output = await w.result()
    print("Final output", output)  # 100

    # stop the agent
    agent.cancel()
    await agent


if __name__ == "__main__":
    asyncio.run(main())

API

Note: not all features are implemented/completed yet.

Primitives

save(f: Callable, compensate_with: Optional[Callable]): Any - memoize the result of a function to avoid re-execution on replay. Functions shall be idempotent as they may be called more than once in case of failure just before the value is persisted. The function can be compensated by providing its inverse effect and trigger later in the workflow with revert.

handle(event_name: str, cb: Callable): None - register a callback to be executed when a specific event is received. The callbacks are executed in the order they were received and whenever a primitive being called.

ensure(f: Callable): True - wait for the function to evaluate to true and schedule a new run when false.

Higher-level

sleep - schedule a replay after a certain amount of time.

receive - wait for the value of an event to be received.

log - TODO

datetime.now - TODO

random - TODO

uuid4 - TODO

Advanced

revert() - execute the compensations and stop the workflow.

continue_using(workflow, *args, **kwargs) - stop the current workflow and pass the context to a new one.

compact() - a key can be defined on all the primitives to avoid the infinitely growing log issue. This function will keep only the last occurrence of each of the keys.

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

substantial-0.1.0.tar.gz (22.4 kB view details)

Uploaded Source

Built Distribution

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

substantial-0.1.0-py3-none-any.whl (26.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: substantial-0.1.0.tar.gz
  • Upload date:
  • Size: 22.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.8 Linux/6.5.0-1025-azure

File hashes

Hashes for substantial-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9db614bcb6dde29f07b1e45e8a443f0c5f3b0d8ead8a866b61cddba21b1631a5
MD5 ab20c3a2643524d86ca3653fd58d5704
BLAKE2b-256 0d1cfc6bbdd049a0a1add6295f339f171743c745c2b5c614dbb937dce1d91e6d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: substantial-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 26.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.8 Linux/6.5.0-1025-azure

File hashes

Hashes for substantial-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 91d0264f4a3b7c2b56ae3738c4cdf163a987877add8712cec90101719307233d
MD5 fbe2f551ea9b74a05e32641a43f10de8
BLAKE2b-256 2a1d2584eed6442421f2ef84ba1eacb26fe2d28cb0d5db37450a55098090d464

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