python_asyncio_course/6_tasks/6_04_01_weather.py

36 lines
936 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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