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

single

Challenge: Your First Async Function

Swipe um das Menü anzuzeigen

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.
Aufgabe

Wischen, um mit dem Codieren zu beginnen

  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().

Lösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 7
single

single

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

some-alt