/
mialdr
/
python_advanced
Обзор
Документация
Войти
/
mialdr
/
python_advanced
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
module_25_asynchronous_programming/homework/hw_1/main.py
41 строка
1 KB
Mikhail
module25 is solved
16 мар 2024, 10:44
16 мар 2024, 10:44
c986738
Код
Авторство
О чём код?
import asyncio from pathlib import Path from module_25_asynchronous_programming.homework.hw_2.main import timer import aiohttp URL = 'https://cataas.com/cat' CATS_WE_WANT = 100 OUT_PATH = Path(__file__).parent / 'cats' OUT_PATH.mkdir(exist_ok=True, parents=True) OUT_PATH = OUT_PATH.absolute() async def get_cat(client: aiohttp.ClientSession, idx: int) -> None: async with client.get(URL) as response: print(response.status) result = await response.read() await asyncio.to_thread(write_to_disk, result, idx) def write_to_disk(content: bytes, id: int): file_path = "{}/{}.png".format(OUT_PATH, id) with open(file_path, mode='wb') as f: f.write(content) async def get_all_cats(): async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(15)) as client: tasks = [get_cat(client, i) for i in range(CATS_WE_WANT)] return await asyncio.gather(*tasks) @timer def main(): res = asyncio.run(get_all_cats()) print(len(res)) if __name__ == '__main__': main()