Skip to content

PubSubClientFactory class

fastpubsub._internal.clients.PubSubClientFactory

Caches the admin clients and the publishers of each event loop.

The cache keeps the creation task of each client, so concurrent first calls for the same key wait for the same task and get the same client.

The class needs no lock on free-threaded Python. Each key holds an event loop, so only the thread of that loop creates or reads the entry, and each single dict operation is atomic.

get_admin_client async classmethod

get_admin_client()

Gets or creates the admin client of the running event loop.

RETURNS DESCRIPTION
AdminClient

The admin client.

Source code in fastpubsub/_internal/clients.py
@classmethod
async def get_admin_client(cls) -> AdminClient:
    """Gets or creates the admin client of the running event loop.

    Returns:
        The admin client.
    """
    loop = asyncio.get_running_loop()
    creation = cls._admin_clients.get(loop)
    if creation is None:
        creation = loop.create_task(
            asyncio.to_thread(AdminClient, Connection.read_endpoint())
        )
        cls._admin_clients[loop] = creation
    return await asyncio.shield(creation)

get_publisher async classmethod

get_publisher(topic_path)

Gets or creates the publisher of a topic for the running loop.

PARAMETER DESCRIPTION
topic_path

The full path of the topic.

TYPE: str

RETURNS DESCRIPTION
Publisher

The publisher of the topic.

Source code in fastpubsub/_internal/clients.py
@classmethod
async def get_publisher(cls, topic_path: str) -> Publisher:
    """Gets or creates the publisher of a topic for the running loop.

    Args:
        topic_path: The full path of the topic.

    Returns:
        The publisher of the topic.
    """
    loop = asyncio.get_running_loop()
    key = (loop, topic_path)
    publisher = cls._publishers.get(key)
    if publisher is not None:
        return publisher

    creation = cls._publisher_creations.get(key)
    if creation is None:
        creation = loop.create_task(
            asyncio.to_thread(
                Publisher,
                topic_path,
                EventLoopBridge.get(),
                EventLoopBridge.resolve_futures,
                Connection.read_endpoint(),
            )
        )
        cls._publisher_creations[key] = creation
    publisher = await asyncio.shield(creation)
    cls._publishers[key] = publisher
    return publisher

close_all async classmethod

close_all()

Sends the pending messages and clears the cache.

Source code in fastpubsub/_internal/clients.py
@classmethod
async def close_all(cls) -> None:
    """Sends the pending messages and clears the cache."""
    creations = list(cls._publisher_creations.values())
    cls._publisher_creations.clear()
    cls._publishers.clear()
    cls._admin_clients.clear()
    EventLoopBridge.clear()
    for creation in creations:
        with suppress(Exception):
            publisher = await creation
            await asyncio.to_thread(publisher.flush)