Skip to content

AsyncPubSubClient

Bases: Subscribe

Asynchronous Redis Publish/Subscribe Client.

Supports channel subscription, unsubscription, pattern-based subscriptions, and listening for incoming messages asynchronously.

Source code in pyredis/client/async_pubsub.py
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class AsyncPubSubClient(commands.Subscribe):
    """
    Asynchronous Redis Publish/Subscribe Client.

    Supports channel subscription, unsubscription, pattern-based subscriptions,
    and listening for incoming messages asynchronously.
    """

    def __init__(self, **kwargs):
        """
        Initialize the AsyncPubSubClient connection.

        Args:
            **kwargs: Connection options forwarded to AsyncConnection.
        """
        self._conn = pyredis.client.AsyncConnection(**kwargs)

    async def close(self):
        """Close the underlying connection asynchronously."""
        await self._conn.close()

    @property
    def closed(self):
        """Flag indicating if the connection is closed."""
        return self._conn.closed

    async def write(self, *args):
        """
        Write a command to the underlying connection asynchronously.

        Args:
            *args: Command name and arguments.
        """
        return await self._conn.write(*args)

    async def get(self):
        """
        Read the next message/response from the connection asynchronously.

        Returns:
            The message or response read from Redis.
        """
        return await self._conn.read(close_on_timeout=False)

closed property

Flag indicating if the connection is closed.

__init__(**kwargs)

Initialize the AsyncPubSubClient connection.

Parameters:

Name Type Description Default
**kwargs

Connection options forwarded to AsyncConnection.

{}
Source code in pyredis/client/async_pubsub.py
13
14
15
16
17
18
19
20
def __init__(self, **kwargs):
    """
    Initialize the AsyncPubSubClient connection.

    Args:
        **kwargs: Connection options forwarded to AsyncConnection.
    """
    self._conn = pyredis.client.AsyncConnection(**kwargs)

close() async

Close the underlying connection asynchronously.

Source code in pyredis/client/async_pubsub.py
22
23
24
async def close(self):
    """Close the underlying connection asynchronously."""
    await self._conn.close()

get() async

Read the next message/response from the connection asynchronously.

Returns:

Type Description

The message or response read from Redis.

Source code in pyredis/client/async_pubsub.py
40
41
42
43
44
45
46
47
async def get(self):
    """
    Read the next message/response from the connection asynchronously.

    Returns:
        The message or response read from Redis.
    """
    return await self._conn.read(close_on_timeout=False)

psubscribe(*args)

Source code in pyredis/commands/subscribe.py
 7
 8
 9
10
def psubscribe(self, *args):
    return self.write(
        *[b"PSUBSCRIBE", *args]
    )

punsubscribe(*args)

Source code in pyredis/commands/subscribe.py
12
13
14
15
def punsubscribe(self, *args):
    return self.write(
        *[b"PUNSUBSCRIBE", *args]
    )

subscribe(*args)

Source code in pyredis/commands/subscribe.py
17
18
19
20
def subscribe(self, *args):
    return self.write(
        *[b"SUBSCRIBE", *args]
    )

unsubscribe(*args)

Source code in pyredis/commands/subscribe.py
22
23
24
25
def unsubscribe(self, *args):
    return self.write(
        *[b"UNSUBSCRIBE", *args]
    )

write(*args) async

Write a command to the underlying connection asynchronously.

Parameters:

Name Type Description Default
*args

Command name and arguments.

()
Source code in pyredis/client/async_pubsub.py
31
32
33
34
35
36
37
38
async def write(self, *args):
    """
    Write a command to the underlying connection asynchronously.

    Args:
        *args: Command name and arguments.
    """
    return await self._conn.write(*args)