|
4 | 4 | I order to avoid cyclic dependences, please |
5 | 5 | DO NOT IMPORT ANYTHING from . |
6 | 6 | """ |
| 7 | + |
7 | 8 | import asyncio |
8 | 9 | import logging |
9 | 10 | import os |
10 | 11 | import socket |
11 | 12 | from collections.abc import Awaitable, Coroutine, Generator, Iterable |
12 | 13 | from pathlib import Path |
13 | | -from typing import Any, Final, cast |
| 14 | +from typing import Any, AsyncGenerator, AsyncIterable, Final, TypeVar, cast |
14 | 15 |
|
15 | 16 | import toolz |
16 | 17 | from pydantic import NonNegativeInt |
17 | 18 |
|
18 | 19 | _logger = logging.getLogger(__name__) |
19 | 20 |
|
| 21 | +_DEFAULT_GATHER_TASKS_GROUP_PREFIX: Final[str] = "gathered" |
| 22 | +_DEFAULT_LOGGER: Final[logging.Logger] = _logger |
| 23 | +_DEFAULT_LIMITED_CONCURRENCY: Final[int] = 1 |
| 24 | + |
20 | 25 |
|
21 | 26 | def is_production_environ() -> bool: |
22 | 27 | """ |
@@ -175,3 +180,144 @@ def unused_port() -> int: |
175 | 180 | with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: |
176 | 181 | s.bind(("127.0.0.1", 0)) |
177 | 182 | return cast(int, s.getsockname()[1]) |
| 183 | + |
| 184 | + |
| 185 | +T = TypeVar("T") |
| 186 | + |
| 187 | + |
| 188 | +async def limited_as_completed( |
| 189 | + awaitables: Iterable[Awaitable[T]] | AsyncIterable[Awaitable[T]], |
| 190 | + *, |
| 191 | + limit: int = _DEFAULT_LIMITED_CONCURRENCY, |
| 192 | + tasks_group_prefix: str | None = None, |
| 193 | +) -> AsyncGenerator[asyncio.Future[T], None]: |
| 194 | + """Runs awaitables using limited concurrent tasks and returns |
| 195 | + result futures unordered. |
| 196 | +
|
| 197 | + Arguments: |
| 198 | + awaitables -- The awaitables to limit the concurrency of. |
| 199 | +
|
| 200 | + Keyword Arguments: |
| 201 | + limit -- The maximum number of awaitables to run concurrently. |
| 202 | + 0 or negative values disables the limit. (default: {1}) |
| 203 | + tasks_group_prefix -- The prefix to use for the name of the asyncio tasks group. |
| 204 | + If None, no name is used. (default: {None}) |
| 205 | +
|
| 206 | + Returns: |
| 207 | + nothing |
| 208 | +
|
| 209 | + Yields: |
| 210 | + Future[T]: the future of the awaitables as they appear. |
| 211 | +
|
| 212 | +
|
| 213 | + """ |
| 214 | + try: |
| 215 | + awaitable_iterator = aiter(awaitables) # type: ignore[arg-type] |
| 216 | + is_async = True |
| 217 | + except TypeError: |
| 218 | + assert isinstance(awaitables, Iterable) # nosec |
| 219 | + awaitable_iterator = iter(awaitables) # type: ignore[assignment] |
| 220 | + is_async = False |
| 221 | + |
| 222 | + completed_all_awaitables = False |
| 223 | + pending_futures: set[asyncio.Future] = set() |
| 224 | + |
| 225 | + try: |
| 226 | + while pending_futures or not completed_all_awaitables: |
| 227 | + while ( |
| 228 | + limit < 1 or len(pending_futures) < limit |
| 229 | + ) and not completed_all_awaitables: |
| 230 | + try: |
| 231 | + aw = ( |
| 232 | + await anext(awaitable_iterator) |
| 233 | + if is_async |
| 234 | + else next(awaitable_iterator) # type: ignore[call-overload] |
| 235 | + ) |
| 236 | + future = asyncio.ensure_future(aw) |
| 237 | + if tasks_group_prefix: |
| 238 | + future.set_name(f"{tasks_group_prefix}-{future.get_name()}") |
| 239 | + pending_futures.add(future) |
| 240 | + except (StopIteration, StopAsyncIteration): # noqa: PERF203 |
| 241 | + completed_all_awaitables = True |
| 242 | + if not pending_futures: |
| 243 | + return |
| 244 | + done, pending_futures = await asyncio.wait( |
| 245 | + pending_futures, return_when=asyncio.FIRST_COMPLETED |
| 246 | + ) |
| 247 | + |
| 248 | + for future in done: |
| 249 | + yield future |
| 250 | + except asyncio.CancelledError: |
| 251 | + for future in pending_futures: |
| 252 | + future.cancel() |
| 253 | + await asyncio.gather(*pending_futures, return_exceptions=True) |
| 254 | + raise |
| 255 | + |
| 256 | + |
| 257 | +async def _wrapped( |
| 258 | + awaitable: Awaitable[T], *, index: int, reraise: bool, logger: logging.Logger |
| 259 | +) -> tuple[int, T | BaseException]: |
| 260 | + try: |
| 261 | + return index, await awaitable |
| 262 | + except asyncio.CancelledError: |
| 263 | + logger.debug( |
| 264 | + "Cancelled %i-th concurrent task %s", |
| 265 | + index + 1, |
| 266 | + f"{awaitable=}", |
| 267 | + ) |
| 268 | + raise |
| 269 | + except BaseException as exc: # pylint: disable=broad-exception-caught |
| 270 | + logger.warning( |
| 271 | + "Error in %i-th concurrent task %s: %s", |
| 272 | + index + 1, |
| 273 | + f"{awaitable=}", |
| 274 | + f"{exc=}", |
| 275 | + ) |
| 276 | + if reraise: |
| 277 | + raise |
| 278 | + return index, exc |
| 279 | + |
| 280 | + |
| 281 | +async def limited_gather( |
| 282 | + *awaitables: Awaitable[T], |
| 283 | + reraise: bool = True, |
| 284 | + log: logging.Logger = _DEFAULT_LOGGER, |
| 285 | + limit: int = _DEFAULT_LIMITED_CONCURRENCY, |
| 286 | + tasks_group_prefix: str | None = None, |
| 287 | +) -> list[T | BaseException | None]: |
| 288 | + """runs all the awaitables using the limited concurrency and returns them in the same order |
| 289 | +
|
| 290 | + Arguments: |
| 291 | + awaitables -- The awaitables to limit the concurrency of. |
| 292 | +
|
| 293 | + Keyword Arguments: |
| 294 | + limit -- The maximum number of awaitables to run concurrently. |
| 295 | + setting 0 or negative values disable (default: {1}) |
| 296 | + reraise -- if True will raise at the first exception |
| 297 | + The remaining tasks will continue as in standard asyncio gather. |
| 298 | + If False, then the exceptions will be returned (default: {True}) |
| 299 | + log -- the logger to use for logging the exceptions (default: {_logger}) |
| 300 | + tasks_group_prefix -- The prefix to use for the name of the asyncio tasks group. |
| 301 | + If None, 'gathered' prefix is used. (default: {None}) |
| 302 | +
|
| 303 | + Returns: |
| 304 | + the results of the awaitables keeping the order |
| 305 | +
|
| 306 | + special thanks to: https://death.andgravity.com/limit-concurrency |
| 307 | + """ |
| 308 | + |
| 309 | + indexed_awaitables = [ |
| 310 | + _wrapped(awaitable, reraise=reraise, index=index, logger=log) |
| 311 | + for index, awaitable in enumerate(awaitables) |
| 312 | + ] |
| 313 | + |
| 314 | + results: list[T | BaseException | None] = [None] * len(indexed_awaitables) |
| 315 | + async for future in limited_as_completed( |
| 316 | + indexed_awaitables, |
| 317 | + limit=limit, |
| 318 | + tasks_group_prefix=tasks_group_prefix or _DEFAULT_GATHER_TASKS_GROUP_PREFIX, |
| 319 | + ): |
| 320 | + index, result = await future |
| 321 | + results[index] = result |
| 322 | + |
| 323 | + return results |
0 commit comments