Module ksqldb_confluent.streamed_query_result

Expand source code
# Copyright 2022 Confluent Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Refer to LICENSE for more information.

from asyncio import Queue, Future
from dataclasses import dataclass

from ksqldb_confluent.row import Row
from ksqldb_confluent.types.schema import Schema


@dataclass
class StreamedQueryResult:
    """
    Represents a streamed result.
    The caller is expected to call poll_async() repeatedly until there is no more
    data and the request is complete.
    """
    query_id: str
    schema: Schema
    row_queue: Queue
    status: Future = Future()

    def is_complete(self) -> bool:
        """
        If the result has completed streaming, and has therefore not failed.

        Returns:
            bool: True, if the result has completed streaming, False otherwise.
        """
        return self.status.done() and not bool(self.status.exception())

    def is_failed(self) -> bool:
        """
        If the result has failed in the middle of streaming and is incomplete.

        Returns:
            bool: True, if the result has failed in the middle of streaming and is incomplete,
            False otherwise.
        """
        return self.row_queue.empty() and self.status.done() and self.status.exception() is not None

    async def poll_async(self) -> Row:
        """
        Polls for a row.

        Returns:
            Row: Returns a row or None if the result is complete.
        """
        if self.is_failed():
            raise self.status.exception()
        row = await self.row_queue.get()
        if row is None and not self.status.done():
            self.status.set_result('FINISHED')
        return row

Classes

class StreamedQueryResult (query_id: str, schema: Schema, row_queue: asyncio.queues.Queue, status: _asyncio.Future = <Future pending>)

Represents a streamed result. The caller is expected to call poll_async() repeatedly until there is no more data and the request is complete.

Expand source code
@dataclass
class StreamedQueryResult:
    """
    Represents a streamed result.
    The caller is expected to call poll_async() repeatedly until there is no more
    data and the request is complete.
    """
    query_id: str
    schema: Schema
    row_queue: Queue
    status: Future = Future()

    def is_complete(self) -> bool:
        """
        If the result has completed streaming, and has therefore not failed.

        Returns:
            bool: True, if the result has completed streaming, False otherwise.
        """
        return self.status.done() and not bool(self.status.exception())

    def is_failed(self) -> bool:
        """
        If the result has failed in the middle of streaming and is incomplete.

        Returns:
            bool: True, if the result has failed in the middle of streaming and is incomplete,
            False otherwise.
        """
        return self.row_queue.empty() and self.status.done() and self.status.exception() is not None

    async def poll_async(self) -> Row:
        """
        Polls for a row.

        Returns:
            Row: Returns a row or None if the result is complete.
        """
        if self.is_failed():
            raise self.status.exception()
        row = await self.row_queue.get()
        if row is None and not self.status.done():
            self.status.set_result('FINISHED')
        return row

Class variables

var query_id : str
var row_queue : asyncio.queues.Queue
var schemaSchema
var status : _asyncio.Future

Methods

def is_complete(self) ‑> bool

If the result has completed streaming, and has therefore not failed.

Returns

bool
True, if the result has completed streaming, False otherwise.
Expand source code
def is_complete(self) -> bool:
    """
    If the result has completed streaming, and has therefore not failed.

    Returns:
        bool: True, if the result has completed streaming, False otherwise.
    """
    return self.status.done() and not bool(self.status.exception())
def is_failed(self) ‑> bool

If the result has failed in the middle of streaming and is incomplete.

Returns

bool
True, if the result has failed in the middle of streaming and is incomplete,

False otherwise.

Expand source code
def is_failed(self) -> bool:
    """
    If the result has failed in the middle of streaming and is incomplete.

    Returns:
        bool: True, if the result has failed in the middle of streaming and is incomplete,
        False otherwise.
    """
    return self.row_queue.empty() and self.status.done() and self.status.exception() is not None
async def poll_async(self) ‑> Row

Polls for a row.

Returns

Row
Returns a row or None if the result is complete.
Expand source code
async def poll_async(self) -> Row:
    """
    Polls for a row.

    Returns:
        Row: Returns a row or None if the result is complete.
    """
    if self.is_failed():
        raise self.status.exception()
    row = await self.row_queue.get()
    if row is None and not self.status.done():
        self.status.set_result('FINISHED')
    return row