|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Example: Local Development Without Redis |
| 4 | +
|
| 5 | +This example demonstrates using Docket with the in-memory backend for |
| 6 | +local development, prototyping, or situations where you don't have Redis |
| 7 | +available but still want to use Docket's task scheduling features. |
| 8 | +
|
| 9 | +Use cases: |
| 10 | +- Local development on a laptop without Docker/Redis |
| 11 | +- Quick prototyping and experimentation |
| 12 | +- Educational/tutorial environments |
| 13 | +- Desktop applications that need background tasks |
| 14 | +- CI/CD environments without Redis containers |
| 15 | +- Single-process utilities that benefit from task scheduling |
| 16 | +
|
| 17 | +Limitations: |
| 18 | +- Single process only (no distributed workers) |
| 19 | +- Data stored in memory (lost on restart) |
| 20 | +- Performance may differ from real Redis |
| 21 | +
|
| 22 | +To run: |
| 23 | + uv run examples/local_development.py |
| 24 | +""" |
| 25 | + |
| 26 | +import asyncio |
| 27 | +from datetime import datetime, timedelta, timezone |
| 28 | + |
| 29 | +from docket import Docket, Worker |
| 30 | +from docket.dependencies import Perpetual, Retry |
| 31 | + |
| 32 | + |
| 33 | +# Example 1: Simple immediate task |
| 34 | +async def process_file(filename: str) -> None: |
| 35 | + print(f"📄 Processing file: {filename}") |
| 36 | + await asyncio.sleep(0.5) # Simulate work |
| 37 | + print(f"✅ Completed: {filename}") |
| 38 | + |
| 39 | + |
| 40 | +# Example 2: Scheduled task with retry |
| 41 | +async def backup_data(target: str, retry: Retry = Retry(attempts=3)) -> None: |
| 42 | + print(f"💾 Backing up to: {target}") |
| 43 | + await asyncio.sleep(0.3) |
| 44 | + print(f"✅ Backup complete: {target}") |
| 45 | + |
| 46 | + |
| 47 | +# Example 3: Periodic background task |
| 48 | +async def health_check( |
| 49 | + perpetual: Perpetual = Perpetual(every=timedelta(seconds=2), automatic=True), |
| 50 | +) -> None: |
| 51 | + print(f"🏥 Health check at {datetime.now(timezone.utc).strftime('%H:%M:%S')}") |
| 52 | + |
| 53 | + |
| 54 | +async def main(): |
| 55 | + print("🚀 Starting Docket with in-memory backend (no Redis required!)\n") |
| 56 | + |
| 57 | + # Use memory:// URL for in-memory operation |
| 58 | + async with Docket(name="local-dev", url="memory://local-dev") as docket: |
| 59 | + # Register tasks |
| 60 | + docket.register(process_file) |
| 61 | + docket.register(backup_data) |
| 62 | + docket.register(health_check) |
| 63 | + |
| 64 | + # Schedule some immediate tasks |
| 65 | + print("Scheduling immediate tasks...") |
| 66 | + await docket.add(process_file)("report.pdf") |
| 67 | + await docket.add(process_file)("data.csv") |
| 68 | + await docket.add(process_file)("config.json") |
| 69 | + |
| 70 | + # Schedule a future task |
| 71 | + in_two_seconds = datetime.now(timezone.utc) + timedelta(seconds=2) |
| 72 | + print("Scheduling backup for 2 seconds from now...") |
| 73 | + await docket.add(backup_data, when=in_two_seconds)("/tmp/backup") |
| 74 | + |
| 75 | + # The periodic task will be auto-scheduled by the worker |
| 76 | + print("Setting up periodic health check...\n") |
| 77 | + |
| 78 | + # Run worker to process tasks |
| 79 | + print("=" * 60) |
| 80 | + async with Worker(docket, concurrency=2) as worker: |
| 81 | + # Run for 6 seconds to see the periodic task execute a few times |
| 82 | + print("Worker running for 6 seconds...\n") |
| 83 | + try: |
| 84 | + await asyncio.wait_for(worker.run_forever(), timeout=6.0) |
| 85 | + except asyncio.TimeoutError: |
| 86 | + print("\n" + "=" * 60) |
| 87 | + print("✨ Demo complete!") |
| 88 | + |
| 89 | + # Show final state |
| 90 | + snapshot = await docket.snapshot() |
| 91 | + print("\nFinal state:") |
| 92 | + print(f" Snapshot time: {snapshot.taken.strftime('%H:%M:%S')}") |
| 93 | + print(f" Future tasks: {len(snapshot.future)}") |
| 94 | + print(f" Running tasks: {len(snapshot.running)}") |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + asyncio.run(main()) |
0 commit comments