セクション 2. 章 7
single
Challenge: Fetching Data Concurrently
メニューを表示するにはスワイプしてください
You are building a data aggregation script that pulls information about users and their posts from jsonplaceholder.typicode.com. Your goal is to fetch all the data concurrently and produce a summary report.
The following endpoints are available:
https://jsonplaceholder.typicode.com/users/{user_id}– returns a user object withnameandemailfields;https://jsonplaceholder.typicode.com/posts?userId={user_id}– returns a list of posts for a given user.
タスク
スワイプしてコーディングを開始
- Define an async function
fetch_user(client, user_id)that fetches the user object and returns a dict with keysnameandemail. - Define an async function
fetch_post_count(client, user_id)that fetches the posts list for a user and returns the number of posts as an integer. - Define an async function
fetch_user_summary(client, user_id)that:- calls
fetch_user()andfetch_post_count()concurrently usingasyncio.gather(); - returns a formatted string:
"{name} ({email}) – {post_count} posts".
- calls
- Define an async function
main()that:- fetches summaries for user IDs
1through5concurrently usingasyncio.gather(); - prints each summary on a separate line.
- fetches summaries for user IDs
- Run
main()usingasyncio.run().
解答
すべて明確でしたか?
フィードバックありがとうございます!
セクション 2. 章 7
single
AIに質問する
AIに質問する
何でも質問するか、提案された質問の1つを試してチャットを始めてください