Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Async Web Scraper with Rate Limiting | Advanced Control and Debugging
Python Asyncio in Depth
Section 4. Chapter 3
single

single

Challenge: Async Web Scraper with Rate Limiting

Swipe to show menu

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 with title and userId fields;
  • https://jsonplaceholder.typicode.com/comments?postId={post_id} – returns a list of comments for a given post.
Task

Swipe to start coding

  1. Define an async function fetch_post(client, semaphore, post_id) that:
    • uses the provided semaphore to limit concurrency;
    • fetches the post with a 4.0 second timeout using asyncio.wait_for();
    • returns a dict with keys post_id, title, and user_id;
    • returns None on any exception.
  2. Define an async function fetch_comment_count(client, semaphore, post_id) that:
    • uses the provided semaphore to limit concurrency;
    • fetches the comments list with a 4.0 second timeout;
    • returns the number of comments as an integer;
    • returns 0 on any exception.
  3. Define an async function build_report(client, semaphore, post_id) that:
    • calls fetch_post() and fetch_comment_count() concurrently using asyncio.gather();
    • returns None if fetch_post() returned None;
    • otherwise returns a formatted string: "[User {user_id}] {title} – {comment_count} comments".
  4. Define an async function main() that:
    • creates an asyncio.Semaphore with a limit of 5;
    • builds reports for post IDs 1 through 10 concurrently using asyncio.gather();
    • prints each non-None result on a separate line.
  5. Run main() using asyncio.run().

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 3
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

some-alt