|
| 1 | +--- |
| 2 | +title: Duron |
| 3 | +hide: |
| 4 | + - toc |
| 5 | + - navigation |
| 6 | +--- |
| 7 | + |
| 8 | +## Install |
| 9 | + |
| 10 | +Duron requires **Python 3.10+**. |
| 11 | + |
| 12 | +```bash |
| 13 | +pip install git+https://github.com/brian14708/duron.git |
| 14 | +``` |
| 15 | + |
| 16 | +## Quickstart |
| 17 | + |
| 18 | +Duron defines two kinds of functions: |
| 19 | + |
| 20 | +- `@duron.durable` — deterministic orchestration. It replays from logs, ensuring that control flow only advances when every prior step is known. |
| 21 | +- `@duron.effect` — side effects. Wrap anything that touches the outside world (APIs, databases, file I/O). Duron records its return value so it runs once per unique input. |
| 22 | + |
| 23 | +```python |
| 24 | +import asyncio |
| 25 | +import random |
| 26 | +from pathlib import Path |
| 27 | + |
| 28 | +import duron |
| 29 | +from duron.contrib.storage import FileLogStorage |
| 30 | + |
| 31 | + |
| 32 | +@duron.effect |
| 33 | +async def work(name: str) -> str: |
| 34 | + print("⚡ Preparing to greet...") |
| 35 | + await asyncio.sleep(2) # Simulate I/O |
| 36 | + print("⚡ Greeting...") |
| 37 | + return f"Hello, {name}!" |
| 38 | + |
| 39 | + |
| 40 | +@duron.effect |
| 41 | +async def generate_lucky_number() -> int: |
| 42 | + print("⚡ Generating lucky number...") |
| 43 | + await asyncio.sleep(1) # Simulate I/O |
| 44 | + return random.randint(1, 100) |
| 45 | + |
| 46 | + |
| 47 | +@duron.durable |
| 48 | +async def greeting_flow(ctx: duron.Context, name: str) -> str: |
| 49 | + message, lucky_number = await asyncio.gather( |
| 50 | + ctx.run(work, name), ctx.run(generate_lucky_number) |
| 51 | + ) |
| 52 | + return f"{message} Your lucky number is {lucky_number}." |
| 53 | + |
| 54 | + |
| 55 | +async def main(): |
| 56 | + async with greeting_flow.invoke(FileLogStorage(Path("log.jsonl"))) as job: |
| 57 | + await job.start("Alice") |
| 58 | + result = await job.wait() |
| 59 | + print(result) |
| 60 | + |
| 61 | + |
| 62 | +if __name__ == "__main__": |
| 63 | + asyncio.run(main()) |
| 64 | +``` |
0 commit comments