Learning: Python + Async Foundations¶
How to Use This Page¶
- Read each lesson in order.
- Run small experiments after each lesson.
- Keep notes on latency, failures, and throughput changes.
Lesson 1: Fundamentals¶
Learn how the event loop schedules coroutine work and why async is ideal for I/O-heavy agent systems.
What You Learn¶
- How
asyncandawaitcontrol cooperative execution - Why blocking calls break concurrency
- The difference between concurrency and parallelism
Walkthrough¶
- Start with two fake API calls that each sleep for one second.
- Run them sequentially, then concurrently with
asyncio.gather. - Compare total runtime and inspect logs for execution order.
Try It Yourself¶
- Add a third API call and predict runtime before running it.
- Intentionally add one blocking call and observe what slows down.
- Replace the blocking call with a non-blocking async version.
Lesson 2: Intermediate Patterns¶
Learn bounded concurrency, timeout behavior, and retry policies for unstable dependencies.
What You Learn¶
- How semaphores limit in-flight requests
- Why timeouts must be explicit per dependency
- How retries with jitter reduce synchronized failure storms
Walkthrough¶
- Wrap outbound calls in a semaphore.
- Add timeout and retry logic for transient failures.
- Return partial results when one source fails.
Try It Yourself¶
- Test semaphore values of 2, 5, and 10 and compare throughput.
- Simulate random timeout errors and measure retry success rate.
- Track which configuration gives best reliability under load.
Lesson 3: Production Patterns¶
Learn backpressure, graceful degradation, and instrumentation for stable operations.
What You Learn¶
- When to reject or defer work during saturation
- How to preserve critical output while dropping optional enrichment
- Which latency and error metrics matter most in production
Walkthrough¶
- Add a queue limit for incoming work.
- Introduce priority handling for critical tasks.
- Emit per-step latency and error counters.
Try It Yourself¶
- Trigger overload and confirm low-priority work is skipped first.
- Compare median and P95 latency before and after backpressure.
- Document one fallback strategy you would ship to production.
Code Examples¶
- Starter script:
docs/starter/stage01_async_foundations.py - Stage architecture notes:
docs/roadmap/stage-01-async-concurrency/architecture.md