-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercises.py
More file actions
359 lines (290 loc) · 11.9 KB
/
exercises.py
File metadata and controls
359 lines (290 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
"""
Async HTTP — Exercises
=======================
Practice problems to test your understanding of async HTTP patterns.
Try to solve each exercise before looking at the solutions below!
Run this file:
python3 exercises.py
All exercises use SIMULATED async functions (asyncio.sleep-based mocks) so you
don't need aiohttp or any external dependencies. The patterns are identical to
what you'd use with real HTTP — just swap the mock functions for aiohttp calls.
"""
import asyncio
import random
import time
# =============================================================================
# Shared mock functions — these simulate HTTP requests
# =============================================================================
# Use these in your exercises. They're already written for you.
async def mock_fetch(url, delay=0.1):
"""
Simulate fetching a URL. Returns a dict with status, url, and body.
Sleeps for `delay` seconds to simulate network latency.
"""
await asyncio.sleep(delay)
return {
"status": 200,
"url": url,
"body": f"Content from {url}",
}
async def mock_fetch_unreliable(url, delay=0.1, fail_chance=0.5):
"""
Like mock_fetch, but randomly fails to simulate flaky servers.
Raises ConnectionError on failure.
"""
await asyncio.sleep(delay)
if random.random() < fail_chance:
raise ConnectionError(f"Failed to connect to {url}")
return {
"status": 200,
"url": url,
"body": f"Content from {url}",
}
async def mock_fetch_page(url, delay=0.1):
"""
Simulate fetching a web page. Returns a dict with HTML-like content
containing 'links' and 'title' that you can extract data from.
"""
await asyncio.sleep(delay)
# Simulate different pages with different data
page_num = url.split("/")[-1] if "/" in url else "0"
return {
"status": 200,
"url": url,
"title": f"Page {page_num}",
"links": [f"{url}/item/{j}" for j in range(3)],
"word_count": random.randint(100, 2000),
}
# =============================================================================
# Exercise 1: Fetch a single URL
#
# Write an async function `fetch_one` that:
# - Takes a URL string as its argument
# - Calls mock_fetch() to get the response
# - Returns a string in the format: "[STATUS] url — body"
# For example: "[200] https://example.com — Content from https://example.com"
#
# Then call it in exercise_1() and print the result.
# =============================================================================
async def exercise_1():
# YOUR CODE HERE
pass
# =============================================================================
# Exercise 2: Fetch multiple URLs concurrently
#
# Write an async function `fetch_all` that:
# - Takes a list of URL strings
# - Fetches ALL of them concurrently using asyncio.gather()
# - Returns a list of response dicts (the raw return values from mock_fetch)
#
# In exercise_2(), call fetch_all with the provided URLs, then print how
# many were fetched and how long it took.
#
# Hint: 5 URLs at 0.1s each should take ~0.1s total (not ~0.5s)
# =============================================================================
async def exercise_2():
urls = [f"https://api.example.com/data/{i}" for i in range(5)]
# YOUR CODE HERE
pass
# =============================================================================
# Exercise 3: Error handling for failed requests
#
# Write an async function `fetch_with_error_handling` that:
# - Takes a URL string
# - Calls mock_fetch_unreliable() to simulate a flaky server
# - On success: returns {"url": url, "status": "ok", "body": response["body"]}
# - On ConnectionError: returns {"url": url, "status": "error", "error": str(e)}
# - On any other exception: returns {"url": url, "status": "error", "error": "Unknown"}
#
# In exercise_3(), fetch 8 URLs concurrently with this function, then print
# a summary: how many succeeded, how many failed, and which ones failed.
#
# Set random.seed(42) before the gather call for reproducible results.
# =============================================================================
async def exercise_3():
urls = [f"https://api.example.com/resource/{i}" for i in range(8)]
# YOUR CODE HERE
pass
# =============================================================================
# Exercise 4: Rate limiting with Semaphore
#
# Write an async function `fetch_rate_limited` that:
# - Takes a URL and a semaphore
# - Uses the semaphore to limit concurrency
# - Calls mock_fetch(url, delay=0.2) inside the semaphore
# - Returns the response dict
#
# In exercise_4(), create 10 URLs and a Semaphore(3), then fetch them all.
# Time the operation and print the elapsed time.
#
# Think about it: 10 URLs, max 3 at a time, 0.2s each.
# That's ceil(10/3) = 4 batches * 0.2s = ~0.8s total.
# =============================================================================
async def exercise_4():
urls = [f"https://api.example.com/limited/{i}" for i in range(10)]
# YOUR CODE HERE
pass
# =============================================================================
# Exercise 5: Retry with exponential backoff
#
# Write an async function `fetch_with_retry` that:
# - Takes a URL and max_retries (default 3)
# - Tries to call mock_fetch_unreliable(url, delay=0.05, fail_chance=0.6)
# - On failure, waits 0.1 * (2 ** attempt) seconds before retrying
# (attempt 0 waits 0.1s, attempt 1 waits 0.2s, attempt 2 waits 0.4s)
# - Returns the response dict on success
# - Returns None if all retries are exhausted
#
# In exercise_5(), set random.seed(77), then try fetching 5 URLs with retry.
# Print which ones succeeded and which ones gave up.
# =============================================================================
async def exercise_5():
urls = [f"https://api.example.com/flaky/{i}" for i in range(5)]
# YOUR CODE HERE
pass
# =============================================================================
# Exercise 6: Concurrent web scraper simulation
#
# Build a two-phase scraper:
#
# Phase 1 — Fetch pages:
# - Use mock_fetch_page() to fetch these 4 URLs concurrently:
# https://example.com/page/1 through /page/4
# - Each returns a dict with "title", "links", and "word_count"
#
# Phase 2 — Extract and aggregate:
# - Collect ALL the links from ALL the pages into a flat list
# - Calculate the total word count across all pages
# - Find the page with the highest word count
#
# Print a summary:
# - Total pages fetched
# - Total links found across all pages
# - Total word count
# - Which page had the most words
#
# Bonus: Use a Semaphore(2) so only 2 pages are fetched at a time.
#
# Set random.seed(55) before fetching for reproducible word counts.
# =============================================================================
async def exercise_6():
urls = [f"https://example.com/page/{i}" for i in range(1, 5)]
# YOUR CODE HERE
pass
# =============================================================================
# Solutions (no peeking until you've tried!)
# =============================================================================
async def solution_1():
async def fetch_one(url):
response = await mock_fetch(url)
return f"[{response['status']}] {response['url']} — {response['body']}"
result = await fetch_one("https://api.example.com/hello")
print(f" {result}")
async def solution_2():
async def fetch_all(urls):
tasks = [mock_fetch(url) for url in urls]
return await asyncio.gather(*tasks)
urls = [f"https://api.example.com/data/{i}" for i in range(5)]
start = time.time()
results = await fetch_all(urls)
elapsed = time.time() - start
print(f" Fetched {len(results)} URLs in {elapsed:.2f}s")
for r in results:
print(f" [{r['status']}] {r['url']}")
async def solution_3():
async def fetch_with_error_handling(url):
try:
response = await mock_fetch_unreliable(url)
return {"url": url, "status": "ok", "body": response["body"]}
except ConnectionError as e:
return {"url": url, "status": "error", "error": str(e)}
except Exception:
return {"url": url, "status": "error", "error": "Unknown"}
urls = [f"https://api.example.com/resource/{i}" for i in range(8)]
random.seed(42)
results = await asyncio.gather(*[fetch_with_error_handling(url) for url in urls])
successes = [r for r in results if r["status"] == "ok"]
failures = [r for r in results if r["status"] == "error"]
print(f" Results: {len(successes)} succeeded, {len(failures)} failed")
for r in failures:
print(f" FAILED: {r['url']} — {r['error']}")
async def solution_4():
async def fetch_rate_limited(url, semaphore):
async with semaphore:
return await mock_fetch(url, delay=0.2)
urls = [f"https://api.example.com/limited/{i}" for i in range(10)]
semaphore = asyncio.Semaphore(3)
start = time.time()
tasks = [fetch_rate_limited(url, semaphore) for url in urls]
results = await asyncio.gather(*tasks)
elapsed = time.time() - start
print(f" Fetched {len(results)} URLs in {elapsed:.2f}s (limited to 3 concurrent)")
print(f" Expected ~0.8s (4 batches of 3 at 0.2s each)")
async def solution_5():
async def fetch_with_retry(url, max_retries=3):
for attempt in range(max_retries):
try:
response = await mock_fetch_unreliable(url, delay=0.05, fail_chance=0.6)
return response
except ConnectionError:
wait_time = 0.1 * (2 ** attempt)
if attempt < max_retries - 1:
await asyncio.sleep(wait_time)
return None
urls = [f"https://api.example.com/flaky/{i}" for i in range(5)]
random.seed(77)
results = await asyncio.gather(*[fetch_with_retry(url) for url in urls])
for url, result in zip(urls, results):
if result is not None:
print(f" {url} — succeeded")
else:
print(f" {url} — gave up after retries")
successes = sum(1 for r in results if r is not None)
print(f" Total: {successes}/{len(urls)} succeeded")
async def solution_6():
urls = [f"https://example.com/page/{i}" for i in range(1, 5)]
semaphore = asyncio.Semaphore(2)
async def fetch_limited(url):
async with semaphore:
return await mock_fetch_page(url)
# Phase 1: Fetch pages concurrently
random.seed(55)
tasks = [fetch_limited(url) for url in urls]
pages = await asyncio.gather(*tasks)
# Phase 2: Extract and aggregate
all_links = []
total_words = 0
max_words_page = None
max_words = 0
for page in pages:
all_links.extend(page["links"])
total_words += page["word_count"]
if page["word_count"] > max_words:
max_words = page["word_count"]
max_words_page = page["title"]
print(f" Pages fetched: {len(pages)}")
print(f" Total links found: {len(all_links)}")
print(f" Total word count: {total_words}")
print(f" Most words: {max_words_page} ({max_words} words)")
# =============================================================================
# Run it!
# =============================================================================
async def async_main():
exercises = [
("Fetch a single URL", exercise_1),
("Fetch multiple URLs concurrently", exercise_2),
("Error handling for failed requests", exercise_3),
("Rate limiting with Semaphore", exercise_4),
("Retry with exponential backoff", exercise_5),
("Concurrent web scraper simulation", exercise_6),
]
for i, (title, func) in enumerate(exercises, 1):
print("=" * 50)
print(f"EXERCISE {i}: {title}")
print("=" * 50)
await func()
print()
print("-" * 50)
print("Done! Compare your output with the solutions.")
if __name__ == "__main__":
asyncio.run(async_main())