Skip to content

PubSubStreamingPullTask class

fastpubsub._internal.delivery.streaming_pull.PubSubStreamingPullTask

PubSubStreamingPullTask(subscriber)

A task that runs the handler for each message that the core receives.

The core gives the server twice the max_messages limit, because the server stops the stream too early at the exact limit. The task keeps the max_messages limit itself: it runs at most that many handlers at the same time, and the other messages wait in a queue.

Initializes the PubSubStreamingPullTask.

PARAMETER DESCRIPTION
subscriber

The subscriber to receive messages for.

TYPE: Subscriber

Source code in fastpubsub/_internal/delivery/streaming_pull.py
def __init__(self, subscriber: Subscriber) -> None:
    """Initializes the PubSubStreamingPullTask.

    Args:
        subscriber: The subscriber to receive messages for.
    """
    self.loop = asyncio.get_running_loop()
    self.subscriber = subscriber
    self.subscription: Subscription | None = None
    self.tasks: set[asyncio.Task[Any]] = set()
    self.waiting: deque[ReceivedMessage] = deque()
    self.is_closing = False

loop instance-attribute

subscriber instance-attribute

subscriber = subscriber

subscription instance-attribute

subscription = None

tasks instance-attribute

tasks = set()

waiting instance-attribute

waiting = deque()

is_closing instance-attribute

is_closing = False

start async

start()

Starts the message stream.

Source code in fastpubsub/_internal/delivery/streaming_pull.py
async def start(self) -> None:
    """Starts the message stream."""
    logger.info(
        f"The {self.subscriber.name} handler is waiting for messages."
    )
    self.subscription = await asyncio.to_thread(
        Subscription,
        Connection(self.subscriber.project_id).subscription_path(
            self.subscriber.subscription_name
        ),
        self.subscriber.control_flow_policy.max_messages,
        EventLoopBridge.get(),
        self._dispatch,
        Connection.read_endpoint(),
    )

task_ready

task_ready()

Checks if the task is ready.

RETURNS DESCRIPTION
bool

True if the message stream runs, False otherwise.

Source code in fastpubsub/_internal/delivery/streaming_pull.py
def task_ready(self) -> bool:
    """Checks if the task is ready.

    Returns:
        True if the message stream runs, False otherwise.
    """
    return bool(self.subscription and self.subscription.is_running())

task_alive

task_alive()

Checks if the task is alive.

RETURNS DESCRIPTION
bool

True if the message stream runs, False otherwise.

Source code in fastpubsub/_internal/delivery/streaming_pull.py
def task_alive(self) -> bool:
    """Checks if the task is alive.

    Returns:
        True if the message stream runs, False otherwise.
    """
    return self.task_ready()

shutdown async

shutdown(timeout=30.0)

Stops the stream and waits for the messages in progress.

PARAMETER DESCRIPTION
timeout

The maximum time to wait, in seconds.

TYPE: float DEFAULT: 30.0

Source code in fastpubsub/_internal/delivery/streaming_pull.py
async def shutdown(self, timeout: float = 30.0) -> None:
    """Stops the stream and waits for the messages in progress.

    Args:
        timeout: The maximum time to wait, in seconds.
    """
    logger.info(f"The {self.subscriber.name} handler is turning off...")
    self.is_closing = True
    self._nack_waiting()
    if self.subscription:
        await asyncio.to_thread(self.subscription.shutdown, timeout)
    if self.tasks:
        await asyncio.wait(set(self.tasks), timeout=timeout)
    logger.info(f"The {self.subscriber.name} handler is shutdown...")