|
| 1 | +""" |
| 2 | +Utilities to ease the differences between async and sync code. |
| 3 | +
|
| 4 | +These object offer a similar interface between sync and async versions; the |
| 5 | +script async_to_sync.py will replace the async names with the sync names |
| 6 | +when generating the sync version. |
| 7 | +""" |
| 8 | + |
| 9 | +# Copyright (C) 2023 The Psycopg Team |
| 10 | + |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +import queue |
| 14 | +import asyncio |
| 15 | +import threading |
| 16 | +from typing import Any, Callable, Coroutine, TypeVar, TYPE_CHECKING |
| 17 | + |
| 18 | +from typing_extensions import TypeAlias |
| 19 | + |
| 20 | +Worker: TypeAlias = threading.Thread |
| 21 | +AWorker: TypeAlias = "asyncio.Task[None]" |
| 22 | +T = TypeVar("T") |
| 23 | + |
| 24 | +# Hack required on Python 3.8 because subclassing Queue[T] fails at runtime. |
| 25 | +# https://stackoverflow.com/questions/45414066/mypy-how-to-define-a-generic-subclass |
| 26 | +if TYPE_CHECKING: |
| 27 | + _GQueue: TypeAlias = queue.Queue |
| 28 | + _AGQueue: TypeAlias = asyncio.Queue |
| 29 | + |
| 30 | +else: |
| 31 | + |
| 32 | + class FakeGenericMeta(type): |
| 33 | + def __getitem__(self, item): |
| 34 | + return self |
| 35 | + |
| 36 | + class _GQueue(queue.Queue, metaclass=FakeGenericMeta): |
| 37 | + pass |
| 38 | + |
| 39 | + class _AGQueue(asyncio.Queue, metaclass=FakeGenericMeta): |
| 40 | + pass |
| 41 | + |
| 42 | + |
| 43 | +class Queue(_GQueue[T]): |
| 44 | + """ |
| 45 | + A Queue subclass with an interruptible get() method. |
| 46 | + """ |
| 47 | + |
| 48 | + def get(self, block: bool = True, timeout: float | None = None) -> T: |
| 49 | + # Always specify a timeout to make the wait interruptible. |
| 50 | + if timeout is None: |
| 51 | + timeout = 24.0 * 60.0 * 60.0 |
| 52 | + return super().get(block=block, timeout=timeout) |
| 53 | + |
| 54 | + |
| 55 | +class AQueue(_AGQueue[T]): |
| 56 | + pass |
| 57 | + |
| 58 | + |
| 59 | +def aspawn( |
| 60 | + f: Callable[..., Coroutine[Any, Any, None]], |
| 61 | + args: tuple[Any, ...] = (), |
| 62 | + name: str | None = None, |
| 63 | +) -> asyncio.Task[None]: |
| 64 | + """ |
| 65 | + Equivalent to asyncio.create_task. |
| 66 | + """ |
| 67 | + return asyncio.create_task(f(*args), name=name) |
| 68 | + |
| 69 | + |
| 70 | +def spawn( |
| 71 | + f: Callable[..., Any], |
| 72 | + args: tuple[Any, ...] = (), |
| 73 | + name: str | None = None, |
| 74 | +) -> threading.Thread: |
| 75 | + """ |
| 76 | + Equivalent to creating and running a daemon thread. |
| 77 | + """ |
| 78 | + t = threading.Thread(target=f, args=args, name=name, daemon=True) |
| 79 | + t.start() |
| 80 | + return t |
| 81 | + |
| 82 | + |
| 83 | +async def agather(*tasks: asyncio.Task[Any], timeout: float | None = None) -> None: |
| 84 | + """ |
| 85 | + Equivalent to asyncio.gather or Thread.join() |
| 86 | + """ |
| 87 | + wait = asyncio.gather(*tasks) |
| 88 | + try: |
| 89 | + if timeout is not None: |
| 90 | + await asyncio.wait_for(asyncio.shield(wait), timeout=timeout) |
| 91 | + else: |
| 92 | + await wait |
| 93 | + except asyncio.TimeoutError: |
| 94 | + pass |
| 95 | + else: |
| 96 | + return |
| 97 | + |
| 98 | + |
| 99 | +def gather(*tasks: threading.Thread, timeout: float | None = None) -> None: |
| 100 | + """ |
| 101 | + Equivalent to asyncio.gather or Thread.join() |
| 102 | + """ |
| 103 | + for t in tasks: |
| 104 | + if not t.is_alive(): |
| 105 | + continue |
| 106 | + t.join(timeout) |
0 commit comments