当前位置: 首页>>代码示例>>Python>>正文


Python typing.AsyncGenerator方法代码示例

本文整理汇总了Python中typing.AsyncGenerator方法的典型用法代码示例。如果您正苦于以下问题:Python typing.AsyncGenerator方法的具体用法?Python typing.AsyncGenerator怎么用?Python typing.AsyncGenerator使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在typing的用法示例。


在下文中一共展示了typing.AsyncGenerator方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _subscribe

# 需要导入模块: import typing [as 别名]
# 或者: from typing import AsyncGenerator [as 别名]
def _subscribe(
        self, document: DocumentNode, *args, **kwargs
    ) -> AsyncGenerator[ExecutionResult, None]:

        # Fetch schema from transport if needed and validate document if possible
        await self.fetch_and_validate(document)

        # Subscribe to the transport
        inner_generator: AsyncGenerator[
            ExecutionResult, None
        ] = self.transport.subscribe(document, *args, **kwargs)

        # Keep a reference to the inner generator to allow the user to call aclose()
        # before a break if python version is too old (pypy3 py 3.6.1)
        self._generator = inner_generator

        async for result in inner_generator:
            if result.errors:
                # Note: we need to run generator.aclose() here or the finally block in
                # transport.subscribe will not be reached in pypy3 (py 3.6.1)
                await inner_generator.aclose()

            yield result 
开发者ID:graphql-python,项目名称:gql,代码行数:25,代码来源:client.py

示例2: subscribe

# 需要导入模块: import typing [as 别名]
# 或者: from typing import AsyncGenerator [as 别名]
def subscribe(
        self, document: DocumentNode, *args, **kwargs,
    ) -> AsyncGenerator[ExecutionResult, None]:
        """Send a subscription and receive the results using an async generator

        The results are sent as an ExecutionResult object
        """

        subscribe_result = await subscribe(self.schema, document, *args, **kwargs)

        if isinstance(subscribe_result, ExecutionResult):
            yield subscribe_result

        else:
            async for result in subscribe_result:
                yield result 
开发者ID:graphql-python,项目名称:gql,代码行数:18,代码来源:local_schema.py

示例3: run_sync_iterable

# 需要导入模块: import typing [as 别名]
# 或者: from typing import AsyncGenerator [as 别名]
def run_sync_iterable(iterable: Generator[Any, None, None]) -> AsyncGenerator[Any, None]:
    async def _gen_wrapper() -> AsyncGenerator[Any, None]:
        # Wrap the generator such that each iteration runs
        # in the executor. Then rationalise the raised
        # errors so that it ends.
        def _inner() -> Any:
            # https://bugs.python.org/issue26221
            # StopIteration errors are swallowed by the
            # run_in_exector method
            try:
                return next(iterable)
            except StopIteration:
                raise StopAsyncIteration()

        loop = asyncio.get_running_loop()
        while True:
            try:
                yield await loop.run_in_executor(None, copy_context().run, _inner)
            except StopAsyncIteration:
                return

    return _gen_wrapper() 
开发者ID:pgjones,项目名称:quart,代码行数:24,代码来源:utils.py

示例4: test_stream_with_context

# 需要导入模块: import typing [as 别名]
# 或者: from typing import AsyncGenerator [as 别名]
def test_stream_with_context() -> None:
    app = Quart(__name__)

    @app.route("/")
    async def index() -> AsyncGenerator[bytes, None]:
        @stream_with_context
        async def generator() -> AsyncGenerator[bytes, None]:
            yield request.method.encode()
            yield b" "
            yield request.path.encode()

        return generator()

    test_client = app.test_client()
    response = await test_client.get("/")
    result = await response.get_data(raw=True)
    assert result == b"GET /"  # type: ignore 
开发者ID:pgjones,项目名称:quart,代码行数:19,代码来源:test_helpers.py

示例5: get

# 需要导入模块: import typing [as 别名]
# 或者: from typing import AsyncGenerator [as 别名]
def get(
        self, path: str, required_serial: Optional[int], **kw: Any
    ) -> AsyncGenerator[aiohttp.ClientResponse, None]:
        logger.debug(f"Getting {path} (serial {required_serial})")
        if not path.startswith(("https://", "http://")):
            path = self.url + path

        async with self.session.get(path, **kw) as r:
            got_serial = (
                int(r.headers[PYPI_SERIAL_HEADER])
                if PYPI_SERIAL_HEADER in r.headers
                else None
            )
            await self.check_for_stale_cache(path, required_serial, got_serial)
            yield r

    # TODO: Add storage backend support / refactor - #554 
开发者ID:pypa,项目名称:bandersnatch,代码行数:19,代码来源:master.py

示例6: as_completed_with_async

# 需要导入模块: import typing [as 别名]
# 或者: from typing import AsyncGenerator [as 别名]
def as_completed_with_async(
        self,
        coros: typing.Iterable[typing.Awaitable],
        limit: int = 50,
        raise_exception: bool = True,
    ) -> typing.AsyncGenerator[typing.Any, None]:
        """as_completed`s async version, can catch and log exception inside.
        """
        for coro in self.as_completed(coros, limit=limit):
            try:
                yield await coro
            except Exception as e:
                if raise_exception:
                    raise e
                else:
                    self.logger.exception(
                        "Get exception {:s} in "
                        '"as_completed_with_async"'.format(str(e))
                    ) 
开发者ID:strongbugman,项目名称:ant_nest,代码行数:21,代码来源:pool.py

示例7: __scan

# 需要导入模块: import typing [as 别名]
# 或者: from typing import AsyncGenerator [as 别名]
def __scan(self, path: Union[str, os.PathLike], candidates: Set[str], similarity: float) -> AsyncGenerator[Tuple[str, pathlib.Path], None]:
        """One level depth search generator for application execs based on similarity with candidate names.
        :param path:        root dir of which subdirectories will be scanned
        :param candidates:  set of app names used for exact and close matching with directory names
        :param similarity:  cutoff level for difflib.get_close_matches; set 1 for exact matching only
        :yields:            2-el. tuple of app_name and executable
        """
        root, dirs, _ = next(os.walk(path))
        logging.debug(f'New scan - similarity: {similarity}, candidates: {list(candidates)}')
        for dir_name in dirs:
            await asyncio.sleep(0)
            matches = self.get_close_matches(dir_name, candidates, similarity)
            for app_name in matches:
                dir_path = pathlib.PurePath(root) / dir_name
                best_exe = self.find_best_exe(dir_path, app_name)
                if best_exe is None:
                    logging.warning('No executable found, moving to next best matched app')
                    continue
                candidates.remove(app_name)
                yield app_name, pathlib.Path(best_exe)
                break 
开发者ID:UncleGoogle,项目名称:galaxy-integration-humblebundle,代码行数:23,代码来源:baseappfinder.py

示例8: lock

# 需要导入模块: import typing [as 别名]
# 或者: from typing import AsyncGenerator [as 别名]
def lock(
        self,
        conversation_id: Text,
        lock_lifetime: float = LOCK_LIFETIME,
        wait_time_in_seconds: float = 1,
    ) -> AsyncGenerator[TicketLock, None]:
        """Acquire lock with lifetime `lock_lifetime`for `conversation_id`.

        Try acquiring lock with a wait time of `wait_time_in_seconds` seconds
        between attempts. Raise a `LockError` if lock has expired.
        """

        ticket = self.issue_ticket(conversation_id, lock_lifetime)

        try:
            yield await self._acquire_lock(
                conversation_id, ticket, wait_time_in_seconds
            )

        finally:
            self.cleanup(conversation_id, ticket) 
开发者ID:botfront,项目名称:rasa-for-botfront,代码行数:23,代码来源:lock_store.py

示例9: test_subclassing_async_generator

# 需要导入模块: import typing [as 别名]
# 或者: from typing import AsyncGenerator [as 别名]
def test_subclassing_async_generator(self):
        class G(typing.AsyncGenerator[int, int]):
            def asend(self, value):
                pass
            def athrow(self, typ, val=None, tb=None):
                pass

        ns = {}
        exec('async def g(): yield 0', globals(), ns)
        g = ns['g']
        self.assertIsSubclass(G, typing.AsyncGenerator)
        self.assertIsSubclass(G, typing.AsyncIterable)
        self.assertIsSubclass(G, collections.AsyncGenerator)
        self.assertIsSubclass(G, collections.AsyncIterable)
        self.assertNotIsSubclass(type(g), G)

        instance = G()
        self.assertIsInstance(instance, typing.AsyncGenerator)
        self.assertIsInstance(instance, typing.AsyncIterable)
        self.assertIsInstance(instance, collections.AsyncGenerator)
        self.assertIsInstance(instance, collections.AsyncIterable)
        self.assertNotIsInstance(type(g), G)
        self.assertNotIsInstance(g, G) 
开发者ID:ShikyoKira,项目名称:Project-New-Reign---Nemesis-Main,代码行数:25,代码来源:test_typing.py

示例10: download

# 需要导入模块: import typing [as 别名]
# 或者: from typing import AsyncGenerator [as 别名]
def download(self, file: TypeLocation, file_size: int,
                       part_size_kb: Optional[float] = None,
                       connection_count: Optional[int] = None) -> AsyncGenerator[bytes, None]:
        connection_count = connection_count or self._get_connection_count(file_size)
        part_size = (part_size_kb or utils.get_appropriated_part_size(file_size)) * 1024
        part_count = math.ceil(file_size / part_size)
        log.debug("Starting parallel download: "
                  f"{connection_count} {part_size} {part_count} {file!s}")
        await self._init_download(connection_count, file, part_count, part_size)

        part = 0
        while part < part_count:
            tasks = []
            for sender in self.senders:
                tasks.append(self.loop.create_task(sender.next()))
            for task in tasks:
                data = await task
                if not data:
                    break
                yield data
                part += 1
                log.trace(f"Part {part} downloaded")

        log.debug("Parallel download finished, cleaning up connections")
        await self._cleanup() 
开发者ID:tulir,项目名称:mautrix-telegram,代码行数:27,代码来源:parallel_file_transfer.py

示例11: consume

# 需要导入模块: import typing [as 别名]
# 或者: from typing import AsyncGenerator [as 别名]
def consume(
        self, listen_for: List[Tuple[str, str]], listener_name: str, **kwargs
    ) -> AsyncGenerator[List[EventMessage], None]:
        """Consume messages for the given APIs

        Examples:

            Consuming events::

                listen_for = [
                    ('mycompany.auth', 'user_created'),
                    ('mycompany.auth', 'user_updated'),
                ]
                async with event_transport.consume(listen_for) as event_message:
                    print(event_message)

        """
        raise NotImplementedError(
            f"Event transport {self.__class__.__name__} does not support listening for events"
        ) 
开发者ID:adamcharnock,项目名称:lightbus,代码行数:22,代码来源:base.py

示例12: get_subscription_games

# 需要导入模块: import typing [as 别名]
# 或者: from typing import AsyncGenerator [as 别名]
def get_subscription_games(self, subscription_name: str, context: Any) -> AsyncGenerator[
        List[SubscriptionGame], None]:
        """Override this method to provide SubscriptionGames for a given subscription.
        This method should `yield` a list of SubscriptionGames -> yield [sub_games]

        This method will only be used if :meth:`get_subscriptions` has been implemented.

        :param context: the value returned from :meth:`prepare_subscription_games_context`
        :return a generator object that yields SubscriptionGames

        .. code-block:: python
            :linenos:

            async def get_subscription_games(subscription_name: str, context: Any):
                while True:
                    games_page = await self._get_subscriptions_from_backend(subscription_name, i)
                    if not games_pages:
                        yield None
                    yield [SubGame(game['game_id'], game['game_title']) for game in games_page]

        """
        raise NotImplementedError() 
开发者ID:gogcom,项目名称:galaxy-integrations-python-api,代码行数:24,代码来源:plugin.py

示例13: getiter

# 需要导入模块: import typing [as 别名]
# 或者: from typing import AsyncGenerator [as 别名]
def getiter(
        self,
        url: str,
        url_vars: Dict[str, str] = {},
        *,
        accept: str = sansio.accept_format(),
        jwt: Opt[str] = None,
        oauth_token: Opt[str] = None,
    ) -> AsyncGenerator[Any, None]:
        """Return an async iterable for all the items at a specified endpoint."""
        data, more = await self._make_request(
            "GET", url, url_vars, b"", accept, jwt=jwt, oauth_token=oauth_token
        )

        if isinstance(data, dict) and "items" in data:
            data = data["items"]

        for item in data:
            yield item
        if more:
            # `yield from` is not supported in coroutines.
            async for item in self.getiter(
                more, url_vars, accept=accept, jwt=jwt, oauth_token=oauth_token
            ):
                yield item 
开发者ID:brettcannon,项目名称:gidgethub,代码行数:27,代码来源:abc.py

示例14: get_mongodb_user_db

# 需要导入模块: import typing [as 别名]
# 或者: from typing import AsyncGenerator [as 别名]
def get_mongodb_user_db():
    async def _get_mongodb_user_db(
        user_model,
    ) -> AsyncGenerator[MongoDBUserDatabase, None]:
        client = motor.motor_asyncio.AsyncIOMotorClient(
            "mongodb://localhost:27017",
            serverSelectionTimeoutMS=100,
            uuidRepresentation="standard",
        )

        try:
            await client.server_info()
        except pymongo.errors.ServerSelectionTimeoutError:
            pytest.skip("MongoDB not available", allow_module_level=True)
            return

        db = client["test_database"]
        collection = db["users"]

        yield MongoDBUserDatabase(user_model, collection)

        await collection.drop()

    return _get_mongodb_user_db 
开发者ID:frankie567,项目名称:fastapi-users,代码行数:26,代码来源:test_db_mongodb.py

示例15: sqlalchemy_user_db

# 需要导入模块: import typing [as 别名]
# 或者: from typing import AsyncGenerator [as 别名]
def sqlalchemy_user_db() -> AsyncGenerator[SQLAlchemyUserDatabase, None]:
    Base: DeclarativeMeta = declarative_base()

    class User(SQLAlchemyBaseUserTable, Base):
        first_name = Column(String, nullable=True)

    DATABASE_URL = "sqlite:///./test-sqlalchemy-user.db"
    database = Database(DATABASE_URL)

    engine = sqlalchemy.create_engine(
        DATABASE_URL, connect_args={"check_same_thread": False}
    )
    Base.metadata.create_all(engine)

    await database.connect()

    yield SQLAlchemyUserDatabase(UserDB, database, User.__table__)

    Base.metadata.drop_all(engine) 
开发者ID:frankie567,项目名称:fastapi-users,代码行数:21,代码来源:test_db_sqlalchemy.py


注:本文中的typing.AsyncGenerator方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。