-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
62 lines (44 loc) · 1.78 KB
/
Copy pathmain.py
File metadata and controls
62 lines (44 loc) · 1.78 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import asyncio
import time
from src.utils import REQUESTS, Request
from src.logs import create_logger
from src.connectors import HostResponse, get_request_latency
logger = create_logger("app.log")
async def main():
requests = REQUESTS
max_host_len = max([r.host_len() for r in requests])
tasks = {}
while True:
# Append tasks that
for req in requests:
if req.running:
continue
if time.time() - req.prev_time > req.sleep_s:
task = asyncio.create_task(get_request_latency(req))
tasks[task] = req
req.running = True
if len(tasks) == 0:
await asyncio.sleep(1)
continue
done, pending = await asyncio.wait(
tasks.keys(), timeout=1, return_when=asyncio.FIRST_COMPLETED
)
for task in done:
req: Request = tasks.pop(task)
req.running = False
req.prev_time = time.time()
result: HostResponse = task.result()
if result.is_error():
logger.info(
f"Wifi {result.wifi_name}, Host {result.host.ljust(max_host_len)} latency {result.latency:.2f} ms, host not reachable, Error - {result.get_err_message()}"
)
else:
logger.info(
f"Wifi {result.wifi_name}, Host {result.host.ljust(max_host_len)} latency {result.latency:.2f} ms"
)
if result.dns_resp:
logger.info(
f"Wifi {result.wifi_name}, Host {result.host.ljust(max_host_len)} DNS {result.get_dns_info()} "
)
if __name__ == "__main__":
asyncio.run(main())