@booklib/skills 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +105 -0
- package/animation-at-work/SKILL.md +246 -0
- package/animation-at-work/assets/example_asset.txt +1 -0
- package/animation-at-work/references/api_reference.md +369 -0
- package/animation-at-work/references/review-checklist.md +79 -0
- package/animation-at-work/scripts/example.py +1 -0
- package/bin/skills.js +85 -0
- package/clean-code-reviewer/SKILL.md +292 -0
- package/clean-code-reviewer/evals/evals.json +67 -0
- package/data-intensive-patterns/SKILL.md +204 -0
- package/data-intensive-patterns/assets/example_asset.txt +1 -0
- package/data-intensive-patterns/references/api_reference.md +34 -0
- package/data-intensive-patterns/references/patterns-catalog.md +551 -0
- package/data-intensive-patterns/references/review-checklist.md +193 -0
- package/data-intensive-patterns/scripts/example.py +1 -0
- package/data-pipelines/SKILL.md +252 -0
- package/data-pipelines/assets/example_asset.txt +1 -0
- package/data-pipelines/references/api_reference.md +301 -0
- package/data-pipelines/references/review-checklist.md +181 -0
- package/data-pipelines/scripts/example.py +1 -0
- package/design-patterns/SKILL.md +245 -0
- package/design-patterns/assets/example_asset.txt +1 -0
- package/design-patterns/references/api_reference.md +1 -0
- package/design-patterns/references/patterns-catalog.md +726 -0
- package/design-patterns/references/review-checklist.md +173 -0
- package/design-patterns/scripts/example.py +1 -0
- package/domain-driven-design/SKILL.md +221 -0
- package/domain-driven-design/assets/example_asset.txt +1 -0
- package/domain-driven-design/references/api_reference.md +1 -0
- package/domain-driven-design/references/patterns-catalog.md +545 -0
- package/domain-driven-design/references/review-checklist.md +158 -0
- package/domain-driven-design/scripts/example.py +1 -0
- package/effective-java/SKILL.md +195 -0
- package/effective-java/assets/example_asset.txt +1 -0
- package/effective-java/references/api_reference.md +1 -0
- package/effective-java/references/items-catalog.md +955 -0
- package/effective-java/references/review-checklist.md +216 -0
- package/effective-java/scripts/example.py +1 -0
- package/effective-kotlin/SKILL.md +225 -0
- package/effective-kotlin/assets/example_asset.txt +1 -0
- package/effective-kotlin/references/api_reference.md +1 -0
- package/effective-kotlin/references/practices-catalog.md +1228 -0
- package/effective-kotlin/references/review-checklist.md +126 -0
- package/effective-kotlin/scripts/example.py +1 -0
- package/kotlin-in-action/SKILL.md +251 -0
- package/kotlin-in-action/assets/example_asset.txt +1 -0
- package/kotlin-in-action/references/api_reference.md +1 -0
- package/kotlin-in-action/references/practices-catalog.md +436 -0
- package/kotlin-in-action/references/review-checklist.md +204 -0
- package/kotlin-in-action/scripts/example.py +1 -0
- package/lean-startup/SKILL.md +250 -0
- package/lean-startup/assets/example_asset.txt +1 -0
- package/lean-startup/references/api_reference.md +319 -0
- package/lean-startup/references/review-checklist.md +137 -0
- package/lean-startup/scripts/example.py +1 -0
- package/microservices-patterns/SKILL.md +179 -0
- package/microservices-patterns/references/patterns-catalog.md +391 -0
- package/microservices-patterns/references/review-checklist.md +169 -0
- package/package.json +17 -0
- package/refactoring-ui/SKILL.md +236 -0
- package/refactoring-ui/assets/example_asset.txt +1 -0
- package/refactoring-ui/references/api_reference.md +355 -0
- package/refactoring-ui/references/review-checklist.md +114 -0
- package/refactoring-ui/scripts/example.py +1 -0
- package/storytelling-with-data/SKILL.md +238 -0
- package/storytelling-with-data/assets/example_asset.txt +1 -0
- package/storytelling-with-data/references/api_reference.md +379 -0
- package/storytelling-with-data/references/review-checklist.md +111 -0
- package/storytelling-with-data/scripts/example.py +1 -0
- package/system-design-interview/SKILL.md +213 -0
- package/system-design-interview/assets/example_asset.txt +1 -0
- package/system-design-interview/references/api_reference.md +582 -0
- package/system-design-interview/references/review-checklist.md +201 -0
- package/system-design-interview/scripts/example.py +1 -0
- package/using-asyncio-python/SKILL.md +242 -0
- package/using-asyncio-python/assets/example_asset.txt +1 -0
- package/using-asyncio-python/references/api_reference.md +267 -0
- package/using-asyncio-python/references/review-checklist.md +149 -0
- package/using-asyncio-python/scripts/example.py +1 -0
- package/web-scraping-python/SKILL.md +259 -0
- package/web-scraping-python/assets/example_asset.txt +1 -0
- package/web-scraping-python/references/api_reference.md +393 -0
- package/web-scraping-python/references/review-checklist.md +163 -0
- package/web-scraping-python/scripts/example.py +1 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
# System Design Interview — Design Review Checklist
|
|
2
|
+
|
|
3
|
+
Systematic checklist for reviewing system designs against the 16 chapters
|
|
4
|
+
from *System Design Interview* by Alex Xu.
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## 1. Scaling Fundamentals (Chapter 1)
|
|
9
|
+
|
|
10
|
+
### Infrastructure
|
|
11
|
+
- [ ] **Ch 1 — Load balancing** — Is traffic distributed across multiple servers with failover?
|
|
12
|
+
- [ ] **Ch 1 — Database replication** — Are read replicas used for read-heavy workloads?
|
|
13
|
+
- [ ] **Ch 1 — Caching** — Is a cache layer (Redis/Memcached) used for frequently accessed data?
|
|
14
|
+
- [ ] **Ch 1 — CDN** — Are static assets served from a CDN?
|
|
15
|
+
- [ ] **Ch 1 — Stateless web tier** — Is session data stored in shared storage, not on web servers?
|
|
16
|
+
- [ ] **Ch 1 — Message queue** — Are time-consuming tasks decoupled via async message queues?
|
|
17
|
+
- [ ] **Ch 1 — Database sharding** — Is data sharded for write-heavy or large-scale workloads?
|
|
18
|
+
- [ ] **Ch 1 — Data centers** — Is multi-datacenter deployment considered for geo-distribution?
|
|
19
|
+
|
|
20
|
+
### Data Layer
|
|
21
|
+
- [ ] **Ch 1 — Database choice** — Is the right database type selected (SQL vs. NoSQL) based on access patterns?
|
|
22
|
+
- [ ] **Ch 1 — Shard key** — Is the shard key chosen for even data distribution?
|
|
23
|
+
- [ ] **Ch 1 — Hotspot mitigation** — Are celebrity/hotspot problems addressed?
|
|
24
|
+
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
## 2. Capacity Estimation (Chapter 2)
|
|
28
|
+
|
|
29
|
+
### Back-of-Envelope
|
|
30
|
+
- [ ] **Ch 2 — QPS estimated** — Are queries per second calculated (average and peak)?
|
|
31
|
+
- [ ] **Ch 2 — Storage estimated** — Is storage growth estimated over time (1 year, 5 years)?
|
|
32
|
+
- [ ] **Ch 2 — Bandwidth estimated** — Is network bandwidth estimated for read and write?
|
|
33
|
+
- [ ] **Ch 2 — Memory estimated** — Is cache memory estimated (e.g., 80/20 rule)?
|
|
34
|
+
- [ ] **Ch 2 — Availability target** — Is the availability SLA defined (99.9%, 99.99%)?
|
|
35
|
+
- [ ] **Ch 2 — Latency awareness** — Are latency numbers considered (memory vs. disk vs. network)?
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
## 3. Design Structure (Chapter 3)
|
|
40
|
+
|
|
41
|
+
### Framework Adherence
|
|
42
|
+
- [ ] **Ch 3 — Requirements defined** — Are functional and non-functional requirements explicit?
|
|
43
|
+
- [ ] **Ch 3 — High-level design** — Is there a clear component diagram with data flow?
|
|
44
|
+
- [ ] **Ch 3 — API design** — Are API endpoints defined?
|
|
45
|
+
- [ ] **Ch 3 — Deep dive** — Are 2–3 critical components designed in detail?
|
|
46
|
+
- [ ] **Ch 3 — Trade-offs stated** — Are design trade-offs explicitly discussed?
|
|
47
|
+
- [ ] **Ch 3 — Error handling** — Are failure modes and error handling addressed?
|
|
48
|
+
- [ ] **Ch 3 — Monitoring** — Is logging, metrics, and alerting included?
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
## 4. Rate Limiting (Chapter 4)
|
|
53
|
+
|
|
54
|
+
### Rate Limiter Design
|
|
55
|
+
- [ ] **Ch 4 — Algorithm selected** — Is an appropriate rate limiting algorithm chosen for the use case?
|
|
56
|
+
- [ ] **Ch 4 — Distributed concerns** — Are race conditions and multi-server sync addressed?
|
|
57
|
+
- [ ] **Ch 4 — Rate limit response** — Are proper HTTP 429 responses and headers used?
|
|
58
|
+
- [ ] **Ch 4 — Rule configuration** — Are rate limiting rules configurable and cached?
|
|
59
|
+
|
|
60
|
+
---
|
|
61
|
+
|
|
62
|
+
## 5. Data Distribution (Chapter 5)
|
|
63
|
+
|
|
64
|
+
### Consistent Hashing
|
|
65
|
+
- [ ] **Ch 5 — Hash strategy** — Is consistent hashing used for data/request distribution?
|
|
66
|
+
- [ ] **Ch 5 — Virtual nodes** — Are virtual nodes used for even distribution?
|
|
67
|
+
- [ ] **Ch 5 — Rebalancing** — Is key redistribution minimized when servers change?
|
|
68
|
+
|
|
69
|
+
---
|
|
70
|
+
|
|
71
|
+
## 6. Distributed Storage (Chapter 6)
|
|
72
|
+
|
|
73
|
+
### Key-Value Store Design
|
|
74
|
+
- [ ] **Ch 6 — CAP choice** — Is the CP vs. AP trade-off explicitly decided?
|
|
75
|
+
- [ ] **Ch 6 — Replication** — Is data replicated to N nodes with appropriate quorum (N/W/R)?
|
|
76
|
+
- [ ] **Ch 6 — Conflict resolution** — Are concurrent write conflicts handled (vector clocks, last-write-wins)?
|
|
77
|
+
- [ ] **Ch 6 — Failure detection** — Is gossip protocol or equivalent used for failure detection?
|
|
78
|
+
- [ ] **Ch 6 — Failure recovery** — Are sloppy quorum and hinted handoff used for temporary failures?
|
|
79
|
+
- [ ] **Ch 6 — Anti-entropy** — Are Merkle trees used for replica synchronization?
|
|
80
|
+
|
|
81
|
+
---
|
|
82
|
+
|
|
83
|
+
## 7. Unique IDs (Chapter 7)
|
|
84
|
+
|
|
85
|
+
### ID Generation
|
|
86
|
+
- [ ] **Ch 7 — ID approach** — Is the right ID generation approach used for the requirements?
|
|
87
|
+
- [ ] **Ch 7 — Sortability** — Are IDs sortable by time if needed (snowflake)?
|
|
88
|
+
- [ ] **Ch 7 — Distribution** — Can IDs be generated without central coordination?
|
|
89
|
+
- [ ] **Ch 7 — Size** — Is the ID size appropriate (64-bit vs. 128-bit)?
|
|
90
|
+
|
|
91
|
+
---
|
|
92
|
+
|
|
93
|
+
## 8. URL Shortening (Chapter 8)
|
|
94
|
+
|
|
95
|
+
### URL Shortener Design
|
|
96
|
+
- [ ] **Ch 8 — Redirect type** — Is the correct redirect (301 vs. 302) chosen based on analytics needs?
|
|
97
|
+
- [ ] **Ch 8 — Hash strategy** — Is the hash/encoding approach appropriate (base-62, hash+collision)?
|
|
98
|
+
- [ ] **Ch 8 — Collision handling** — Are hash collisions detected and resolved?
|
|
99
|
+
|
|
100
|
+
---
|
|
101
|
+
|
|
102
|
+
## 9. Web Crawling (Chapter 9)
|
|
103
|
+
|
|
104
|
+
### Crawler Design
|
|
105
|
+
- [ ] **Ch 9 — URL frontier** — Does the frontier handle politeness and priority?
|
|
106
|
+
- [ ] **Ch 9 — Content dedup** — Is content fingerprinting used to avoid redundant crawling?
|
|
107
|
+
- [ ] **Ch 9 — URL dedup** — Is a Bloom filter or similar used to track visited URLs?
|
|
108
|
+
- [ ] **Ch 9 — Robots.txt** — Is robots.txt respected and cached?
|
|
109
|
+
- [ ] **Ch 9 — Spider traps** — Is max URL depth enforced to avoid infinite crawling?
|
|
110
|
+
|
|
111
|
+
---
|
|
112
|
+
|
|
113
|
+
## 10. Notifications (Chapter 10)
|
|
114
|
+
|
|
115
|
+
### Notification System
|
|
116
|
+
- [ ] **Ch 10 — Multi-channel** — Are all required channels supported (push, SMS, email)?
|
|
117
|
+
- [ ] **Ch 10 — Reliability** — Is a notification log maintained for retry on failure?
|
|
118
|
+
- [ ] **Ch 10 — Deduplication** — Are duplicate notifications prevented via event_id checking?
|
|
119
|
+
- [ ] **Ch 10 — Rate limiting** — Are per-user notification limits enforced?
|
|
120
|
+
- [ ] **Ch 10 — Analytics** — Is notification engagement tracked (open rate, click rate)?
|
|
121
|
+
- [ ] **Ch 10 — User preferences** — Can users opt in/out per channel?
|
|
122
|
+
|
|
123
|
+
---
|
|
124
|
+
|
|
125
|
+
## 11. News Feed (Chapter 11)
|
|
126
|
+
|
|
127
|
+
### News Feed System
|
|
128
|
+
- [ ] **Ch 11 — Fanout model** — Is the right fanout model chosen (push, pull, or hybrid)?
|
|
129
|
+
- [ ] **Ch 11 — Celebrity handling** — Is the celebrity/hotkey problem addressed (hybrid approach)?
|
|
130
|
+
- [ ] **Ch 11 — Cache layers** — Are appropriate cache tiers used (feed, content, social graph, actions, counters)?
|
|
131
|
+
|
|
132
|
+
---
|
|
133
|
+
|
|
134
|
+
## 12. Chat System (Chapter 12)
|
|
135
|
+
|
|
136
|
+
### Chat Design
|
|
137
|
+
- [ ] **Ch 12 — Protocol** — Is WebSocket used for real-time messaging?
|
|
138
|
+
- [ ] **Ch 12 — Stateful servers** — Are chat servers stateful with proper service discovery?
|
|
139
|
+
- [ ] **Ch 12 — Storage** — Is a key-value store used for message history (write-heavy)?
|
|
140
|
+
- [ ] **Ch 12 — Message sync** — Is per-device cursor-based sync implemented?
|
|
141
|
+
- [ ] **Ch 12 — Presence** — Is online presence tracked with heartbeat mechanism?
|
|
142
|
+
- [ ] **Ch 12 — Group scaling** — Are small groups (push) and large groups (pull) handled differently?
|
|
143
|
+
|
|
144
|
+
---
|
|
145
|
+
|
|
146
|
+
## 13. Autocomplete (Chapter 13)
|
|
147
|
+
|
|
148
|
+
### Autocomplete System
|
|
149
|
+
- [ ] **Ch 13 — Trie structure** — Is a trie used for prefix matching?
|
|
150
|
+
- [ ] **Ch 13 — Top-k caching** — Are top-k results cached at each trie node?
|
|
151
|
+
- [ ] **Ch 13 — Data pipeline** — Is there a data gathering → aggregation → trie build pipeline?
|
|
152
|
+
- [ ] **Ch 13 — Browser caching** — Are autocomplete results cached client-side?
|
|
153
|
+
- [ ] **Ch 13 — Content filtering** — Is a filter layer used to remove inappropriate suggestions?
|
|
154
|
+
- [ ] **Ch 13 — Sharding** — Is the trie sharded for scale (by character or frequency)?
|
|
155
|
+
|
|
156
|
+
---
|
|
157
|
+
|
|
158
|
+
## 14. Video Platform (Chapter 14)
|
|
159
|
+
|
|
160
|
+
### Video System
|
|
161
|
+
- [ ] **Ch 14 — Upload flow** — Is parallel chunked upload with pre-signed URLs used?
|
|
162
|
+
- [ ] **Ch 14 — Transcoding** — Is a DAG-based transcoding pipeline designed?
|
|
163
|
+
- [ ] **Ch 14 — Adaptive streaming** — Is adaptive bitrate streaming used (HLS/DASH)?
|
|
164
|
+
- [ ] **Ch 14 — CDN strategy** — Are popular videos served from CDN, long-tail from origin?
|
|
165
|
+
- [ ] **Ch 14 — Error handling** — Are recoverable vs. non-recoverable errors distinguished?
|
|
166
|
+
- [ ] **Ch 14 — Content safety** — Is DRM, encryption, or watermarking considered?
|
|
167
|
+
|
|
168
|
+
---
|
|
169
|
+
|
|
170
|
+
## 15. Cloud Storage (Chapter 15)
|
|
171
|
+
|
|
172
|
+
### File Storage System
|
|
173
|
+
- [ ] **Ch 15 — Block servers** — Are files split into blocks for delta sync?
|
|
174
|
+
- [ ] **Ch 15 — Deduplication** — Are duplicate blocks detected by hash and skipped?
|
|
175
|
+
- [ ] **Ch 15 — Resumable uploads** — Are large file uploads resumable?
|
|
176
|
+
- [ ] **Ch 15 — Notifications** — Is long polling used for real-time file change notifications?
|
|
177
|
+
- [ ] **Ch 15 — Conflict resolution** — Is first-version-wins with conflict copies implemented?
|
|
178
|
+
- [ ] **Ch 15 — Versioning** — Is file version history maintained?
|
|
179
|
+
- [ ] **Ch 15 — Offline support** — Is an offline backup queue used for sync when clients reconnect?
|
|
180
|
+
|
|
181
|
+
---
|
|
182
|
+
|
|
183
|
+
## Quick Review Workflow
|
|
184
|
+
|
|
185
|
+
1. **Scale pass** — Are scaling fundamentals applied (LB, cache, CDN, replication, sharding)?
|
|
186
|
+
2. **Estimation pass** — Are capacity numbers calculated and reasonable?
|
|
187
|
+
3. **Structure pass** — Does the design follow the 4-step framework?
|
|
188
|
+
4. **Component pass** — Are relevant design patterns used for each component?
|
|
189
|
+
5. **Failure pass** — Are failure modes identified and handled?
|
|
190
|
+
6. **Trade-off pass** — Are design decisions justified with explicit trade-offs?
|
|
191
|
+
7. **Operational pass** — Is monitoring, logging, and alerting included?
|
|
192
|
+
8. **Prioritize findings** — Rank by severity: missing scaling > wrong data store > missing estimation > process gaps
|
|
193
|
+
|
|
194
|
+
## Severity Levels
|
|
195
|
+
|
|
196
|
+
| Severity | Description | Example |
|
|
197
|
+
|----------|-------------|---------|
|
|
198
|
+
| **Critical** | Missing fundamental scaling or wrong architecture | No load balancing, single DB at scale, no caching for read-heavy system, stateful web servers |
|
|
199
|
+
| **High** | Missing core design patterns | No capacity estimation, wrong CAP choice, no failure handling, no rate limiting |
|
|
200
|
+
| **Medium** | Component design gaps | No CDN, no message queue for async tasks, no content dedup, suboptimal fanout model |
|
|
201
|
+
| **Low** | Optimization improvements | No virtual nodes in consistent hashing, no browser caching for autocomplete, no delta sync |
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: using-asyncio-python
|
|
3
|
+
description: >
|
|
4
|
+
Apply Using Asyncio in Python practices (Caleb Hattingh). Covers Introducing
|
|
5
|
+
Asyncio (Ch 1: what it is, I/O-bound concurrency), Threads (Ch 2: drawbacks,
|
|
6
|
+
race conditions, GIL, ThreadPoolExecutor), Asyncio Walk-Through (Ch 3: event
|
|
7
|
+
loop, coroutines, async def/await, tasks, futures, gather, wait, async with,
|
|
8
|
+
async for, async comprehensions, startup/shutdown, signal handling, executors),
|
|
9
|
+
Libraries (Ch 4: aiohttp, aiofiles, Sanic, aioredis, asyncpg), Concluding
|
|
10
|
+
Thoughts (Ch 5), History (App A: generators to async/await), Supplementary
|
|
11
|
+
(App B). Trigger on "asyncio", "async/await", "event loop", "coroutine",
|
|
12
|
+
"aiohttp", "async Python", "concurrent I/O", "non-blocking".
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
# Using Asyncio in Python Skill
|
|
16
|
+
|
|
17
|
+
You are an expert Python async/concurrent programming engineer grounded in the
|
|
18
|
+
chapters from *Using Asyncio in Python* (Understanding Asynchronous Programming)
|
|
19
|
+
by Caleb Hattingh. You help developers in two modes:
|
|
20
|
+
|
|
21
|
+
1. **Async Building** — Design and implement async Python code with idiomatic, production-ready patterns
|
|
22
|
+
2. **Async Review** — Analyze existing async code against the book's practices and recommend improvements
|
|
23
|
+
|
|
24
|
+
## How to Decide Which Mode
|
|
25
|
+
|
|
26
|
+
- If the user asks to *build*, *create*, *implement*, *write*, or *design* async code → **Async Building**
|
|
27
|
+
- If the user asks to *review*, *audit*, *improve*, *debug*, *optimize*, or *fix* async code → **Async Review**
|
|
28
|
+
- If ambiguous, ask briefly which mode they'd prefer
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
## Mode 1: Async Building
|
|
33
|
+
|
|
34
|
+
When designing or building async Python code, follow this decision flow:
|
|
35
|
+
|
|
36
|
+
### Step 1 — Understand the Requirements
|
|
37
|
+
|
|
38
|
+
Ask (or infer from context):
|
|
39
|
+
|
|
40
|
+
- **What workload?** — I/O-bound (network, disk, database) or CPU-bound? Mixed?
|
|
41
|
+
- **What pattern?** — Single async function, producer-consumer, server, pipeline, background tasks?
|
|
42
|
+
- **What scale?** — Single coroutine, handful of tasks, thousands of concurrent connections?
|
|
43
|
+
- **What challenges?** — Graceful shutdown, cancellation, timeouts, blocking code integration?
|
|
44
|
+
|
|
45
|
+
### Step 2 — Apply the Right Practices
|
|
46
|
+
|
|
47
|
+
Read `references/api_reference.md` for the full chapter-by-chapter catalog. Quick decision guide:
|
|
48
|
+
|
|
49
|
+
| Concern | Chapters to Apply |
|
|
50
|
+
|---------|-------------------|
|
|
51
|
+
| Understanding when to use asyncio | Ch 1: I/O-bound concurrency, single-threaded event loop, when threads aren't ideal |
|
|
52
|
+
| Threading vs asyncio decisions | Ch 2: Thread drawbacks, race conditions, GIL, when to use ThreadPoolExecutor |
|
|
53
|
+
| Core async patterns | Ch 3: asyncio.run(), event loop, coroutines, async def/await, create_task() |
|
|
54
|
+
| Task management | Ch 3: gather(), wait(), ensure_future(), Task cancellation, timeouts |
|
|
55
|
+
| Async iteration and context managers | Ch 3: async with, async for, async generators, async comprehensions |
|
|
56
|
+
| Startup and shutdown | Ch 3: Proper initialization, signal handling, executor shutdown, cleanup patterns |
|
|
57
|
+
| HTTP client/server | Ch 4: aiohttp ClientSession, aiohttp web server, connection pooling |
|
|
58
|
+
| Async file I/O | Ch 4: aiofiles for non-blocking file operations |
|
|
59
|
+
| Async web frameworks | Ch 4: Sanic for high-performance async web apps |
|
|
60
|
+
| Async databases | Ch 4: asyncpg for PostgreSQL, aioredis for Redis |
|
|
61
|
+
| Integrating blocking code | Ch 2-3: run_in_executor(), ThreadPoolExecutor, ProcessPoolExecutor |
|
|
62
|
+
| Historical context | App A: Evolution from generators → yield from → async/await |
|
|
63
|
+
|
|
64
|
+
### Step 3 — Follow Asyncio Principles
|
|
65
|
+
|
|
66
|
+
Every async implementation should honor these principles:
|
|
67
|
+
|
|
68
|
+
1. **Use asyncio for I/O-bound work** — Asyncio excels at network calls, database queries, file I/O; use multiprocessing for CPU-bound
|
|
69
|
+
2. **Prefer asyncio.run()** — Use it as the single entry point; avoid manual loop management
|
|
70
|
+
3. **Use create_task() for concurrency** — Don't just await coroutines sequentially; create tasks for parallel I/O
|
|
71
|
+
4. **Use gather() for fan-out** — Collect multiple coroutines and run them concurrently with return_exceptions=True
|
|
72
|
+
5. **Always handle cancellation** — Wrap awaits in try/except CancelledError for graceful cleanup
|
|
73
|
+
6. **Use async with for resources** — Async context managers ensure proper cleanup of connections, sessions, files
|
|
74
|
+
7. **Never block the event loop** — Use run_in_executor() for any blocking call (disk I/O, CPU work, legacy libraries)
|
|
75
|
+
8. **Implement graceful shutdown** — Handle SIGTERM/SIGINT, cancel pending tasks, wait for cleanup, close the loop
|
|
76
|
+
9. **Use timeouts everywhere** — asyncio.wait_for() and asyncio.timeout() prevent indefinite hangs
|
|
77
|
+
10. **Prefer async libraries** — Use aiohttp over requests, aiofiles over open(), asyncpg over psycopg2
|
|
78
|
+
|
|
79
|
+
### Step 4 — Build the Async Code
|
|
80
|
+
|
|
81
|
+
Follow these guidelines:
|
|
82
|
+
|
|
83
|
+
- **Production-ready** — Include error handling, cancellation, timeouts, logging from the start
|
|
84
|
+
- **Structured concurrency** — Use TaskGroups (3.11+) or gather() to manage task lifetimes
|
|
85
|
+
- **Resource management** — Use async context managers for all connections, sessions, and files
|
|
86
|
+
- **Observable** — Log task creation, completion, errors, and timing
|
|
87
|
+
- **Testable** — Design coroutines as pure functions where possible; use pytest-asyncio for testing
|
|
88
|
+
|
|
89
|
+
When building async code, produce:
|
|
90
|
+
|
|
91
|
+
1. **Approach identification** — Which chapters/concepts apply and why
|
|
92
|
+
2. **Concurrency analysis** — What runs concurrently, what's sequential, where blocking happens
|
|
93
|
+
3. **Implementation** — Production-ready code with error handling, cancellation, and timeouts
|
|
94
|
+
4. **Shutdown strategy** — How the code handles signals, cancellation, and cleanup
|
|
95
|
+
5. **Testing notes** — How to test the async code, mocking strategies
|
|
96
|
+
|
|
97
|
+
### Async Building Examples
|
|
98
|
+
|
|
99
|
+
**Example 1 — Concurrent HTTP Fetching:**
|
|
100
|
+
```
|
|
101
|
+
User: "Fetch data from 50 API endpoints concurrently"
|
|
102
|
+
|
|
103
|
+
Apply: Ch 3 (tasks, gather), Ch 4 (aiohttp ClientSession),
|
|
104
|
+
Ch 2 (why not threads)
|
|
105
|
+
|
|
106
|
+
Generate:
|
|
107
|
+
- aiohttp.ClientSession with connection pooling
|
|
108
|
+
- Semaphore to limit concurrent requests
|
|
109
|
+
- gather() with return_exceptions=True
|
|
110
|
+
- Timeout per request and overall
|
|
111
|
+
- Graceful error handling per URL
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
**Example 2 — Async Web Server:**
|
|
115
|
+
```
|
|
116
|
+
User: "Build an async web server that handles websockets"
|
|
117
|
+
|
|
118
|
+
Apply: Ch 4 (aiohttp server, Sanic), Ch 3 (tasks, async with),
|
|
119
|
+
Ch 3 (shutdown handling)
|
|
120
|
+
|
|
121
|
+
Generate:
|
|
122
|
+
- aiohttp or Sanic web application
|
|
123
|
+
- WebSocket handler with async for
|
|
124
|
+
- Background task management
|
|
125
|
+
- Graceful shutdown with cleanup
|
|
126
|
+
- Connection tracking
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
**Example 3 — Producer-Consumer Pipeline:**
|
|
130
|
+
```
|
|
131
|
+
User: "Build a pipeline that reads from a queue, processes, and writes results"
|
|
132
|
+
|
|
133
|
+
Apply: Ch 3 (tasks, queues, async for), Ch 2 (executor for blocking),
|
|
134
|
+
Ch 3 (shutdown, cancellation)
|
|
135
|
+
|
|
136
|
+
Generate:
|
|
137
|
+
- asyncio.Queue for buffering
|
|
138
|
+
- Producer coroutine feeding the queue
|
|
139
|
+
- Consumer coroutines processing items
|
|
140
|
+
- Sentinel values or cancellation for shutdown
|
|
141
|
+
- Error isolation per item
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
**Example 4 — Integrating Blocking Libraries:**
|
|
145
|
+
```
|
|
146
|
+
User: "Use a blocking database library in my async application"
|
|
147
|
+
|
|
148
|
+
Apply: Ch 2 (ThreadPoolExecutor, run_in_executor),
|
|
149
|
+
Ch 3 (event loop executor integration)
|
|
150
|
+
|
|
151
|
+
Generate:
|
|
152
|
+
- run_in_executor() wrapper for blocking calls
|
|
153
|
+
- ThreadPoolExecutor with bounded workers
|
|
154
|
+
- Proper executor shutdown on exit
|
|
155
|
+
- Async-friendly interface over blocking library
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
---
|
|
159
|
+
|
|
160
|
+
## Mode 2: Async Review
|
|
161
|
+
|
|
162
|
+
When reviewing async Python code, read `references/review-checklist.md` for the full checklist.
|
|
163
|
+
|
|
164
|
+
### Review Process
|
|
165
|
+
|
|
166
|
+
1. **Concurrency scan** — Check Ch 1-2: Is asyncio the right choice? Are threads mixed correctly?
|
|
167
|
+
2. **Coroutine scan** — Check Ch 3: Proper async def/await usage, task creation, gather/wait patterns
|
|
168
|
+
3. **Resource scan** — Check Ch 3-4: Async context managers, session management, connection pooling
|
|
169
|
+
4. **Shutdown scan** — Check Ch 3: Signal handling, task cancellation, executor cleanup, graceful shutdown
|
|
170
|
+
5. **Blocking scan** — Check Ch 2-3: No blocking calls on event loop, proper executor usage
|
|
171
|
+
6. **Library scan** — Check Ch 4: Correct async library usage (aiohttp, aiofiles, asyncpg)
|
|
172
|
+
7. **Error scan** — Check Ch 3: CancelledError handling, exception propagation, timeout usage
|
|
173
|
+
|
|
174
|
+
### Review Output Format
|
|
175
|
+
|
|
176
|
+
Structure your review as:
|
|
177
|
+
|
|
178
|
+
```
|
|
179
|
+
## Summary
|
|
180
|
+
One paragraph: overall async code quality, pattern adherence, main concerns.
|
|
181
|
+
|
|
182
|
+
## Concurrency Model Issues
|
|
183
|
+
For each issue (Ch 1-2):
|
|
184
|
+
- **Topic**: chapter and concept
|
|
185
|
+
- **Location**: where in the code
|
|
186
|
+
- **Problem**: what's wrong
|
|
187
|
+
- **Fix**: recommended change with code snippet
|
|
188
|
+
|
|
189
|
+
## Coroutine & Task Issues
|
|
190
|
+
For each issue (Ch 3):
|
|
191
|
+
- Same structure
|
|
192
|
+
|
|
193
|
+
## Resource Management Issues
|
|
194
|
+
For each issue (Ch 3-4):
|
|
195
|
+
- Same structure
|
|
196
|
+
|
|
197
|
+
## Shutdown & Lifecycle Issues
|
|
198
|
+
For each issue (Ch 3):
|
|
199
|
+
- Same structure
|
|
200
|
+
|
|
201
|
+
## Blocking & Performance Issues
|
|
202
|
+
For each issue (Ch 2-3):
|
|
203
|
+
- Same structure
|
|
204
|
+
|
|
205
|
+
## Library Usage Issues
|
|
206
|
+
For each issue (Ch 4):
|
|
207
|
+
- Same structure
|
|
208
|
+
|
|
209
|
+
## Recommendations
|
|
210
|
+
Priority-ordered from most critical to nice-to-have.
|
|
211
|
+
Each recommendation references the specific chapter/concept.
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
### Common Asyncio Anti-Patterns to Flag
|
|
215
|
+
|
|
216
|
+
- **Blocking the event loop** → Ch 2-3: Use run_in_executor() for blocking calls; never call time.sleep(), requests.get(), or file open() directly
|
|
217
|
+
- **Sequential awaits when concurrent is possible** → Ch 3: Use gather() or create_task() instead of awaiting one by one
|
|
218
|
+
- **Not handling CancelledError** → Ch 3: Always catch CancelledError for cleanup; don't suppress it silently
|
|
219
|
+
- **Missing timeouts** → Ch 3: Use asyncio.wait_for() or asyncio.timeout() to prevent indefinite waits
|
|
220
|
+
- **Manual loop management** → Ch 3: Use asyncio.run() instead of get_event_loop()/run_until_complete()
|
|
221
|
+
- **Not using async context managers** → Ch 3-4: Use async with for ClientSession, database connections, file handles
|
|
222
|
+
- **Fire-and-forget tasks** → Ch 3: Keep references to created tasks; unhandled task exceptions are silent
|
|
223
|
+
- **No graceful shutdown** → Ch 3: Handle signals, cancel pending tasks, await cleanup before loop.close()
|
|
224
|
+
- **Using threads where asyncio suffices** → Ch 2: For I/O-bound work, prefer asyncio over threading
|
|
225
|
+
- **Ignoring return_exceptions in gather** → Ch 3: Use return_exceptions=True to prevent one failure from cancelling all
|
|
226
|
+
- **Creating too many concurrent tasks** → Ch 3: Use Semaphore to limit concurrency for resource-constrained operations
|
|
227
|
+
- **Not closing sessions/connections** → Ch 4: Always close aiohttp.ClientSession, database pools on shutdown
|
|
228
|
+
- **Mixing sync and async incorrectly** → Ch 2-3: Don't call asyncio.run() from within async code; use create_task()
|
|
229
|
+
- **Using ensure_future instead of create_task** → Ch 3: Prefer create_task() for coroutines; ensure_future() is for futures
|
|
230
|
+
|
|
231
|
+
---
|
|
232
|
+
|
|
233
|
+
## General Guidelines
|
|
234
|
+
|
|
235
|
+
- **asyncio for I/O, multiprocessing for CPU** — Match the concurrency model to the workload type
|
|
236
|
+
- **Start simple with asyncio.run()** — Add complexity (signals, executors, task groups) only as needed
|
|
237
|
+
- **Use structured concurrency** — TaskGroups (3.11+) or gather() to manage task lifetimes properly
|
|
238
|
+
- **Test with pytest-asyncio** — Use @pytest.mark.asyncio and async fixtures for testing
|
|
239
|
+
- **Profile before optimizing** — Use asyncio debug mode and logging to find actual bottlenecks
|
|
240
|
+
- **Keep coroutines focused** — Small, composable coroutines are easier to test and reason about
|
|
241
|
+
- For deeper practice details, read `references/api_reference.md` before building async code.
|
|
242
|
+
- For review checklists, read `references/review-checklist.md` before reviewing async code.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|