Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Challenge: Your First Async Function | The Asyncio Foundation
Python Asyncio in Depth
Section 1. Chapitre 7
single

single

Challenge: Your First Async Function

Glissez pour afficher le menu

You are working with the public API jsonplaceholder.typicode.com, which provides fake data for testing and learning. Your task is to fetch user information and display it in a readable format.

The endpoint https://jsonplaceholder.typicode.com/users/{user_id} returns a JSON object with the following fields:

  • name: the user's full name;
  • email: the user's email address;
  • company: an object with a name field containing the company name.
Tâche

Glissez pour commencer à coder

  1. Define an async function fetch_user(client, user_id) that:
    • Sends a GET request to the users endpoint using the provided client and user_id;
    • Extracts name, email, and company name from the response;
    • Prints them in the format: Name | Email | Company.
  2. Define an async function main() that:
    • Creates an httpx.AsyncClient;
    • Uses asyncio.gather() to fetch users with IDs 1, 2, and 3 concurrently.
  3. Run main() using asyncio.run().

Solution

Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 7
single

single

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

some-alt