Challenge: Async Weather Reporter
In many real-world applications, you need to fetch data from several sources at once. Imagine you are building a weather dashboard that shows the current temperature for multiple cities. Instead of waiting for each city’s data to arrive one after another, you can use Python’s asyncio to fetch all the data concurrently, making your program much faster and more responsive. By simulating each weather data fetch as an asynchronous function that waits a random amount of time, you can see how asyncio.gather lets you run all these tasks together and collect their results as soon as they’re ready.
Swipe to start coding
Simulate fetching weather data for several cities concurrently using asynchronous functions. Each city's weather is "fetched" with a random simulated delay. Use asyncio.gather to run all fetches together and collect their results. After all weather data is fetched, print the results for each city.
- Each city's weather should be fetched by calling
fetch_weatherwith the city name. - All fetches should run concurrently using
asyncio.gather. - After fetching, print the weather result for each city.
Oplossing
Bedankt voor je feedback!
single
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Geweldig!
Completion tarief verbeterd naar 9.09
Challenge: Async Weather Reporter
Veeg om het menu te tonen
In many real-world applications, you need to fetch data from several sources at once. Imagine you are building a weather dashboard that shows the current temperature for multiple cities. Instead of waiting for each city’s data to arrive one after another, you can use Python’s asyncio to fetch all the data concurrently, making your program much faster and more responsive. By simulating each weather data fetch as an asynchronous function that waits a random amount of time, you can see how asyncio.gather lets you run all these tasks together and collect their results as soon as they’re ready.
Swipe to start coding
Simulate fetching weather data for several cities concurrently using asynchronous functions. Each city's weather is "fetched" with a random simulated delay. Use asyncio.gather to run all fetches together and collect their results. After all weather data is fetched, print the results for each city.
- Each city's weather should be fetched by calling
fetch_weatherwith the city name. - All fetches should run concurrently using
asyncio.gather. - After fetching, print the weather result for each city.
Oplossing
Bedankt voor je feedback!
single