36 lines
936 B
Python
36 lines
936 B
Python
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())
|