File: asyncgen.py

package info (click to toggle)
python-scrapy 2.13.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,664 kB
  • sloc: python: 52,028; xml: 199; makefile: 25; sh: 7
file content (22 lines) | stat: -rw-r--r-- 561 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
from __future__ import annotations

from collections.abc import AsyncGenerator, AsyncIterator, Iterable
from typing import TypeVar

_T = TypeVar("_T")


async def collect_asyncgen(result: AsyncIterator[_T]) -> list[_T]:
    return [x async for x in result]


async def as_async_generator(
    it: Iterable[_T] | AsyncIterator[_T],
) -> AsyncGenerator[_T]:
    """Wraps an iterable (sync or async) into an async generator."""
    if isinstance(it, AsyncIterator):
        async for r in it:
            yield r
    else:
        for r in it:
            yield r