|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import typing |
| 4 | +from typing import Awaitable, Callable, ParamSpec, TypeVar |
| 5 | + |
| 6 | +import anyio |
| 7 | +import anyio.abc |
| 8 | + |
| 9 | +Args = typing.TypeVarTuple("Args") |
| 10 | +T = typing.TypeVar("T") |
| 11 | +U = typing.TypeVar("U") |
| 12 | + |
| 13 | + |
| 14 | +class FutureFinishedWithExceptionError(Exception): |
| 15 | + """An error raised when a future finished with an exception.""" |
| 16 | + |
| 17 | + |
| 18 | +class FutureNotSetError(Exception): |
| 19 | + """An error raised when a future is not set.""" |
| 20 | + |
| 21 | + |
| 22 | +class SimpleFuture[T]: |
| 23 | + """A simple anyio-based future. |
| 24 | +
|
| 25 | + Futures can hold a single result, which is provided by an async task. Following anyio (rather |
| 26 | + than asyncio) conventions, every task is associated with a task group. Futures allow us to |
| 27 | + more easily pass around "boxes" that will eventually contain results. |
| 28 | +
|
| 29 | + Note that if you just want to wait for a set of tasks to complete, you can just use an anyio |
| 30 | + task group, and store the result in a mutable container like a list or dict; you don't need |
| 31 | + futures the same way you do in asyncio. This class is most useful when either: |
| 32 | + - you only have a single result of each type and don't want to deal with creating a list with |
| 33 | + one element, or want the more convenient typechecked interface of SimpleFuture, |
| 34 | + - you want to pass a box between two tasks, so that one task can set the result and another |
| 35 | + can wait for it to be set, without requiring a more heavyweight mechanism like a queue. |
| 36 | +
|
| 37 | + A basic low-level pattern you can use to run a task and get its result later: |
| 38 | +
|
| 39 | + ```python |
| 40 | + async with anyio.create_task_group() as tg: |
| 41 | + future = SimpleFuture() |
| 42 | +
|
| 43 | + async def go(*args): |
| 44 | + future.set_result(await some_task_fn(*args)) |
| 45 | +
|
| 46 | + tg.start_soon(go, *args) |
| 47 | +
|
| 48 | + # do something else asynchronously while the task runs |
| 49 | +
|
| 50 | + # once you exit the task group, the future will have a result |
| 51 | + value = future.get() |
| 52 | + ``` |
| 53 | +
|
| 54 | + This pattern is useful enough that there is a wrapper so that you can instead do: |
| 55 | +
|
| 56 | + ```python |
| 57 | + async with anyio.create_task_group() as tg: |
| 58 | + future = future_from_start_soon(tg, some_task_fn, *args) |
| 59 | + # do something else asynchronously while the task runs |
| 60 | +
|
| 61 | + # once you exit the task group, the future will have a result |
| 62 | + value = future.get() |
| 63 | + ``` |
| 64 | +
|
| 65 | + You can also use `await future.wait_for_result()` to wait for the future to complete and get |
| 66 | + the result. |
| 67 | + """ |
| 68 | + |
| 69 | + def __init__(self): |
| 70 | + self.event = anyio.Event() |
| 71 | + self.result = None |
| 72 | + self.exception = None |
| 73 | + |
| 74 | + def set_result(self, result: T): |
| 75 | + """Set the result of the future.""" |
| 76 | + |
| 77 | + assert not self.event.is_set(), "Result already set" |
| 78 | + self.result = result |
| 79 | + self.event.set() |
| 80 | + |
| 81 | + def set_from_task( |
| 82 | + self, |
| 83 | + task_fn: Callable[[*Args], typing.Awaitable[T]], |
| 84 | + set_exception: bool = True, |
| 85 | + catch_exception: bool = False, |
| 86 | + ) -> Callable[[*Args], typing.Awaitable[None]]: |
| 87 | + """Wrap a callable to set the result of the future from its result. |
| 88 | +
|
| 89 | + This function can be used to wrap a task that returns a value into a task that sets a |
| 90 | + future. It is most useful in combination with `TaskGroup.start_soon`, e.g. |
| 91 | +
|
| 92 | + ```python |
| 93 | + future = SimpleFuture() |
| 94 | + async with anyio.create_task_group() as tg: |
| 95 | + # runs `await some_task_fn(*args)` and sets the future to the result |
| 96 | + tg.start_soon(future.set_from_task(some_task_fn), *args) |
| 97 | +
|
| 98 | + # or, same pattern but with keyword arguments |
| 99 | + tg.start_soon(future.set_from_task(functools.partial(some_task_fn, **kwargs))) |
| 100 | + ``` |
| 101 | +
|
| 102 | + Args: |
| 103 | + task_fn: A callable that returns an awaitable. |
| 104 | + set_exception: Whether to set the future to the exception if the task raises an |
| 105 | + exception. If False, the exception will not be set. |
| 106 | + catch_exception: Whether to catch exceptions and set the future to the exception. If |
| 107 | + False (the default), exceptions will be propagated up to the task group. If True, |
| 108 | + exceptions will be caught and will not cancel the task group. `set_exception` must |
| 109 | + be True if `catch_exception` is True. |
| 110 | +
|
| 111 | + Returns: |
| 112 | + A callable that can be used to start a task that sets the future to the result of |
| 113 | + `task_fn`. |
| 114 | + """ |
| 115 | + if catch_exception and not set_exception: |
| 116 | + raise ValueError("set_exception must be True if catch_exception is True") |
| 117 | + |
| 118 | + async def wrapped_task_fn(*args: *Args): |
| 119 | + try: |
| 120 | + result = await task_fn(*args) |
| 121 | + except Exception as e: |
| 122 | + if set_exception: |
| 123 | + self.set_exception(e) |
| 124 | + if not catch_exception: |
| 125 | + raise |
| 126 | + else: |
| 127 | + raise |
| 128 | + else: |
| 129 | + self.set_result(result) |
| 130 | + |
| 131 | + return wrapped_task_fn |
| 132 | + |
| 133 | + def set_exception(self, exception: Exception): |
| 134 | + """Set the result of the future to an exception.""" |
| 135 | + assert not self.event.is_set(), "Result already set" |
| 136 | + self.exception = exception |
| 137 | + self.event.set() |
| 138 | + |
| 139 | + def has_result(self) -> bool: |
| 140 | + """Check if the future has a result.""" |
| 141 | + return self.event.is_set() |
| 142 | + |
| 143 | + async def wait_for_result(self) -> T: |
| 144 | + """Wait for the future to complete and return the result. |
| 145 | +
|
| 146 | + If the future has an exception, it will be raised. |
| 147 | + """ |
| 148 | + await self.event.wait() |
| 149 | + if self.exception: |
| 150 | + raise FutureFinishedWithExceptionError( |
| 151 | + f"Future finished with an exception (of type {type(self.exception).__name__})!" |
| 152 | + ) from self.exception |
| 153 | + assert self.result is not None |
| 154 | + return self.result |
| 155 | + |
| 156 | + def get(self) -> T: |
| 157 | + """Synchronously get the result of the future. |
| 158 | +
|
| 159 | + If the future has an exception, it will be raised. |
| 160 | + """ |
| 161 | + if not self.has_result(): |
| 162 | + raise FutureNotSetError("Result not set") |
| 163 | + if self.exception: |
| 164 | + raise self.exception |
| 165 | + assert self.result is not None |
| 166 | + return self.result |
| 167 | + |
| 168 | + |
| 169 | +def future_from_start_soon[ |
| 170 | + T, *Args |
| 171 | +]( |
| 172 | + task_group: anyio.abc.TaskGroup, |
| 173 | + task_fn: Callable[[*Args], Awaitable[T]], |
| 174 | + *args: *Args, |
| 175 | + catch_exception: bool = False, |
| 176 | +) -> SimpleFuture[T]: |
| 177 | + """Create a future from the result of starting a coroutine in a task group. |
| 178 | +
|
| 179 | + This is a convenience function for creating a future and starting a task in a task group. |
| 180 | + It can be used like this: |
| 181 | +
|
| 182 | + ```python |
| 183 | + async with anyio.create_task_group() as tg: |
| 184 | + future = future_from_start_soon(tg, some_task_fn, *args) |
| 185 | + ``` |
| 186 | +
|
| 187 | + Args: |
| 188 | + task_group: The task group to start the task in. This is the task group that owns the |
| 189 | + task and will be cancelled if the task raises an exception (unless |
| 190 | + `catch_exception` is True). |
| 191 | + task_fn: The coroutine to start. |
| 192 | + *args: The arguments to pass to the coroutine. |
| 193 | + catch_exception: Whether to catch exceptions and set the future to the exception. If |
| 194 | + False (the default), exceptions will be propagated up to the task group. If True, |
| 195 | + exceptions will be caught and stored in the future instead, and the task group |
| 196 | + will not be cancelled. |
| 197 | +
|
| 198 | + Returns: |
| 199 | + A future that will eventually contain the result of the coroutine, or an exception if |
| 200 | + the coroutine raises an exception and `catch_exception` is True. |
| 201 | + """ |
| 202 | + result = SimpleFuture[T]() |
| 203 | + task_group.start_soon( |
| 204 | + result.set_from_task(task_fn, set_exception=True, catch_exception=catch_exception), |
| 205 | + *args, |
| 206 | + ) |
| 207 | + return result |
| 208 | + |
| 209 | + |
| 210 | +P = ParamSpec("P") # full parameter list of the wrapped function |
| 211 | +T = TypeVar("T") # its return type |
| 212 | + |
0 commit comments