File: utils.py

package info (click to toggle)
python-tiny-proxy 0.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 212 kB
  • sloc: python: 1,154; makefile: 3
file content (56 lines) | stat: -rw-r--r-- 1,469 bytes parent folder | download
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
import asyncio
import socket
import time
from typing import Iterable


def is_connectable(host, port):
    sock = None
    try:
        sock = socket.create_connection((host, port), timeout=1)
    except socket.error:
        return False
    else:
        return True
    finally:
        if sock is not None:
            sock.close()


def wait_until_connectable(host, port, timeout=10):
    count = 0
    while not is_connectable(host=host, port=port):
        if count >= timeout:
            raise Exception(
                f'The proxy server has not available by ({host}, {port}) in {timeout:d} seconds'
            )
        count += 1
        time.sleep(1)
    return True


def cancel_tasks(tasks: Iterable[asyncio.Task], loop: asyncio.AbstractEventLoop):
    if not tasks:
        return

    for task in tasks:
        task.cancel()

    loop.run_until_complete(asyncio.gather(*tasks, return_exceptions=True))

    for task in tasks:
        if task.cancelled():
            continue
        if task.exception() is not None:
            loop.call_exception_handler(
                {
                    "message": "unhandled exception during asyncio.run() shutdown",
                    "exception": task.exception(),
                    "task": task,
                }
            )


def cancel_all_tasks(loop: asyncio.AbstractEventLoop):
    tasks = [task for task in asyncio.all_tasks(loop) if not task.done()]
    cancel_tasks(tasks=tasks, loop=loop)