|
| 1 | +""" |
| 2 | +Async support for pluggy using greenlets. |
| 3 | +
|
| 4 | +This module provides async functionality for pluggy, allowing hook |
| 5 | +implementations to return awaitable objects that are automatically awaited |
| 6 | +when running in an async context (see :meth:`PluginManager.run_async |
| 7 | +<pluggy.PluginManager.run_async>`). |
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +from collections.abc import AsyncGenerator |
| 13 | +from collections.abc import Awaitable |
| 14 | +from collections.abc import Callable |
| 15 | +from collections.abc import Generator |
| 16 | +from typing import Any |
| 17 | +from typing import cast |
| 18 | +from typing import Final |
| 19 | +from typing import TYPE_CHECKING |
| 20 | +from typing import TypeVar |
| 21 | + |
| 22 | + |
| 23 | +_T = TypeVar("_T") |
| 24 | +_Y = TypeVar("_Y") |
| 25 | +_S = TypeVar("_S") |
| 26 | + |
| 27 | +if TYPE_CHECKING: |
| 28 | + import greenlet |
| 29 | + |
| 30 | + |
| 31 | +#: Sentinel distinguishing "worker never completed" from a legitimate |
| 32 | +#: ``None`` result. |
| 33 | +_UNSET: Final = object() |
| 34 | + |
| 35 | + |
| 36 | +class Submitter: |
| 37 | + """Bridge between synchronous hook execution and an async event loop. |
| 38 | +
|
| 39 | + While :meth:`run` is active, awaitables passed to :meth:`maybe_submit` or |
| 40 | + :meth:`require_await` are switched to the awaiting parent greenlet, which |
| 41 | + awaits them and switches the result back. When inactive, |
| 42 | + :meth:`maybe_submit` passes awaitables through unchanged |
| 43 | + ("await-me-maybe"). |
| 44 | + """ |
| 45 | + |
| 46 | + _active_submitter: greenlet.greenlet | None |
| 47 | + |
| 48 | + def __init__(self) -> None: |
| 49 | + self._active_submitter = None |
| 50 | + |
| 51 | + def __repr__(self) -> str: |
| 52 | + return f"<Submitter active={self._active_submitter is not None}>" |
| 53 | + |
| 54 | + def maybe_submit(self, coro: Awaitable[_T]) -> _T | Awaitable[_T]: |
| 55 | + """Await an awaitable if active, else return it unchanged. |
| 56 | +
|
| 57 | + This enables backward compatibility for datasette |
| 58 | + and https://simonwillison.net/2020/Sep/2/await-me-maybe/ |
| 59 | + """ |
| 60 | + active = self._active_submitter |
| 61 | + if active is not None: |
| 62 | + # We're in a greenlet context; switch to the parent with the |
| 63 | + # awaitable. The parent awaits it and switches back the result. |
| 64 | + res: _T = active.switch(coro) |
| 65 | + return res |
| 66 | + else: |
| 67 | + return coro |
| 68 | + |
| 69 | + def require_await(self, coro: Awaitable[_T]) -> _T: |
| 70 | + """Await an awaitable, raising an error if not in async context.""" |
| 71 | + active = self._active_submitter |
| 72 | + if active is not None: |
| 73 | + res: _T = active.switch(coro) |
| 74 | + return res |
| 75 | + else: |
| 76 | + raise RuntimeError("require_await called outside of async context") |
| 77 | + |
| 78 | + async def run(self, sync_func: Callable[[], _T]) -> _T: |
| 79 | + """Run a synchronous function in a worker greenlet with async support. |
| 80 | +
|
| 81 | + Awaitables submitted by hook implementations during the call are |
| 82 | + awaited on this coroutine's event loop. |
| 83 | + """ |
| 84 | + try: |
| 85 | + import greenlet |
| 86 | + except ImportError: |
| 87 | + raise RuntimeError("greenlet is required for async support") from None |
| 88 | + |
| 89 | + if self._active_submitter is not None: |
| 90 | + raise RuntimeError("Submitter is already active") |
| 91 | + |
| 92 | + main_greenlet = greenlet.getcurrent() |
| 93 | + result: object = _UNSET |
| 94 | + exception: BaseException | None = None |
| 95 | + |
| 96 | + def greenlet_func() -> None: |
| 97 | + nonlocal result, exception |
| 98 | + try: |
| 99 | + result = sync_func() |
| 100 | + except BaseException as e: |
| 101 | + exception = e |
| 102 | + |
| 103 | + worker_greenlet = greenlet.greenlet(greenlet_func) |
| 104 | + # Let maybe_submit/require_await switch back to this greenlet. |
| 105 | + self._active_submitter = main_greenlet |
| 106 | + try: |
| 107 | + # Run the worker; every switch back carries an awaitable to |
| 108 | + # process, until the worker finishes (switching back None). |
| 109 | + awaitable = worker_greenlet.switch() |
| 110 | + while awaitable is not None: |
| 111 | + try: |
| 112 | + awaited_result = await awaitable |
| 113 | + except BaseException as e: |
| 114 | + # Raise at the submission site inside the worker. |
| 115 | + awaitable = worker_greenlet.throw(e) |
| 116 | + else: |
| 117 | + awaitable = worker_greenlet.switch(awaited_result) |
| 118 | + finally: |
| 119 | + self._active_submitter = None |
| 120 | + |
| 121 | + if exception is not None: |
| 122 | + raise exception |
| 123 | + assert result is not _UNSET, "worker greenlet did not complete" |
| 124 | + return cast(_T, result) |
| 125 | + |
| 126 | + |
| 127 | +def async_generator_to_sync( |
| 128 | + async_gen: AsyncGenerator[_Y, _S], submitter: Submitter |
| 129 | +) -> Generator[_Y, _S, None]: |
| 130 | + """Convert an async generator to a sync generator using a `Submitter`. |
| 131 | +
|
| 132 | + This helper allows wrapper implementations to use async generators while |
| 133 | + maintaining compatibility with the sync generator interface expected by |
| 134 | + the hook system. The submitter must be active (i.e. the generator must be |
| 135 | + consumed under :meth:`Submitter.run`). |
| 136 | + """ |
| 137 | + try: |
| 138 | + # Start the async generator. |
| 139 | + value = submitter.require_await(async_gen.__anext__()) |
| 140 | + |
| 141 | + while True: |
| 142 | + try: |
| 143 | + sent_value = yield value |
| 144 | + try: |
| 145 | + value = submitter.require_await(async_gen.asend(sent_value)) |
| 146 | + except StopAsyncIteration: |
| 147 | + return |
| 148 | + |
| 149 | + except GeneratorExit: |
| 150 | + # Generator is being closed; close the async generator too. |
| 151 | + submitter.require_await(cast(Awaitable[Any], async_gen.aclose())) |
| 152 | + raise |
| 153 | + |
| 154 | + except BaseException as exc: |
| 155 | + # Exception was thrown into the generator; forward it into |
| 156 | + # the async generator. |
| 157 | + try: |
| 158 | + value = submitter.require_await(async_gen.athrow(exc)) |
| 159 | + except StopAsyncIteration: |
| 160 | + return |
| 161 | + |
| 162 | + except StopAsyncIteration: |
| 163 | + return |
0 commit comments