-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13_asyncio_io.py
More file actions
42 lines (33 loc) · 1.41 KB
/
Copy path13_asyncio_io.py
File metadata and controls
42 lines (33 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""
Тема: asyncio, IO-bound (см. theory/python.md → «Asyncio»).
Дано: функция, эмулирующая сбор данных с сайтов в синхронном варианте
(ожидание ответа = time.sleep).
Задача: переписать load_responses_async так, чтобы все ожидания шли
конкурентно через asyncio, а суммарное время ≈ времени самого долгого сайта,
а не сумме всех.
Подумай: почему здесь нельзя использовать time.sleep внутри корутины
и почему requests не подходит для async-кода?
"""
import asyncio
import time
def load_responses_sync(sites: list[tuple[str, float]]) -> list[str]:
responses = []
for site, timeout in sites:
time.sleep(timeout) # fake wait for response
responses.append(f'{site} OK')
return responses
async def load_responses_async(sites: list[tuple[str, float]]) -> list[str]:
raise NotImplemented
# Тестирование
WEBSITES = [
("google.com", 1.5),
("github.com", 5.2),
("stackoverflow.com", 10.8),
("python.org", 8.5),
("wikipedia.org", 10.0),
("reddit.com", 5.0),
("medium.com", 4.5),
("dev.to", 10.8),
]
print('benchmark', load_responses_sync(WEBSITES))
print('async', asyncio.run(load_responses_async(WEBSITES)))