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