Add solution for 6.4 section

This commit is contained in:
pro100ton 2024-11-14 21:22:15 +03:00
parent 73bf523925
commit 9066c3cad7

36
6_tasks/6_4_01_weather.py Normal file
View file

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