From 9066c3cad7174a982958b7a4fe6f493657993ff7 Mon Sep 17 00:00:00 2001 From: pro100ton Date: Thu, 14 Nov 2024 21:22:15 +0300 Subject: [PATCH] Add solution for 6.4 section --- 6_tasks/6_4_01_weather.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 6_tasks/6_4_01_weather.py diff --git a/6_tasks/6_4_01_weather.py b/6_tasks/6_4_01_weather.py new file mode 100644 index 0000000..e9c0e34 --- /dev/null +++ b/6_tasks/6_4_01_weather.py @@ -0,0 +1,36 @@ +import asyncio +import random + +# Не менять! +random.seed(0) + + +async def fetch_weather(source, city): + await asyncio.sleep(random.randint(1, 5)) + temperature = random.randint(-10, 35) + return ( + f"Данные о погоде получены из источника {source} для " + f"города {city}: {temperature}°C" + ) + + +async def main(): + city = "Москва" + sources = [ + "http://api.weatherapi.com", + "http://api.openweathermap.org", + "http://api.weatherstack.com", + "http://api.weatherbit.io", + "http://api.meteostat.net", + "http://api.climacell.co", + ] + tasks = [ + asyncio.create_task(fetch_weather(source_entry, city)) + for source_entry in sources + ] + done, tasks = await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED) + for done_task in done: + print(done_task.result()) + + +asyncio.run(main())