セクション 4. 章 3
single
Challenge: Async Web Scraper with Rate Limiting
メニューを表示するにはスワイプしてください
You are building a production-ready async data pipeline that fetches posts and their comments from jsonplaceholder.typicode.com. The pipeline must handle concurrency, apply rate limiting, enforce timeouts, and gracefully recover from individual failures.
The following endpoints are available:
https://jsonplaceholder.typicode.com/posts/{post_id}– returns a post object withtitleanduserIdfields;https://jsonplaceholder.typicode.com/comments?postId={post_id}– returns a list of comments for a given post.
タスク
スワイプしてコーディングを開始
- Define an async function
fetch_post(client, semaphore, post_id)that:- uses the provided
semaphoreto limit concurrency; - fetches the post with a
4.0second timeout usingasyncio.wait_for(); - returns a dict with keys
post_id,title, anduser_id; - returns
Noneon any exception.
- uses the provided
- Define an async function
fetch_comment_count(client, semaphore, post_id)that:- uses the provided
semaphoreto limit concurrency; - fetches the comments list with a
4.0second timeout; - returns the number of comments as an integer;
- returns
0on any exception.
- uses the provided
- Define an async function
build_report(client, semaphore, post_id)that:- calls
fetch_post()andfetch_comment_count()concurrently usingasyncio.gather(); - returns
Noneiffetch_post()returnedNone; - otherwise returns a formatted string:
"[User {user_id}] {title} – {comment_count} comments".
- calls
- Define an async function
main()that:- creates an
asyncio.Semaphorewith a limit of5; - builds reports for post IDs
1through10concurrently usingasyncio.gather(); - prints each non-
Noneresult on a separate line.
- creates an
- Run
main()usingasyncio.run().
解答
すべて明確でしたか?
フィードバックありがとうございます!
セクション 4. 章 3
single
AIに質問する
AIに質問する
何でも質問するか、提案された質問の1つを試してチャットを始めてください