Skip to content

PubSubClient

Bases: Subscribe

Synchronous Redis Publish/Subscribe Client.

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

Source code in pyredis/client/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 PubSubClient(commands.Subscribe):
    """
    Synchronous Redis Publish/Subscribe Client.

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

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

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

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

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

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

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

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

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

closed property

Flag indicating if the connection is closed.

__init__(**kwargs)

Initialize the PubSubClient connection.

Parameters:

Name Type Description Default
**kwargs

Connection options forwarded to Connection.

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

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

close()

Close the underlying connection.

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

get()

Read the next message/response from the connection.

Returns:

Type Description

The message or response read from Redis.

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

    Returns:
        The message or response read from Redis.
    """
    return 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)

Write a command to the underlying connection.

Parameters:

Name Type Description Default
*args

Command name and arguments.

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

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