Skip to content

Standalone Batch

Home / python / shared / standalone_batch

glide_shared.commands.batch.Batch

Bases: BaseBatch

Batch implementation for standalone GlideClient. Batches allow the execution of a group of commands in a single step.

Batch Response

An array of command responses is returned by the client exec command, in the order they were given. Each element in the array represents a command given to the Batch. The response for each command depends on the executed Valkey command. Specific response types are documented alongside each method.

Parameters:

Name Type Description Default
is_atomic bool

Determines whether the batch is atomic or non-atomic. If True, the batch will be executed as an atomic transaction. If False, the batch will be executed as a non-atomic pipeline.

required

See Valkey Transactions (Atomic Batches) and Valkey Pipelines (Non-Atomic Batches) for details.

Note for Standalone Mode (Cluster Mode Disabled): Standalone Batches are executed on the primary node.

Examples:

Atomic Batch - Transaction:
>>> transaction = Batch(is_atomic=True)  # Atomic (Transaction)
>>> transaction.set("key", "value")
>>> transaction.get("key")
>>> result = await client.exec(transaction, false)
>>> print(result)
[OK, b"value"]
Non-Atomic Batch - Pipeline:
>>> pipeline = Batch(is_atomic=False)  # Non-Atomic (Pipeline)
>>> pipeline.set("key1", "value1")
>>> pipeline.set("key2", "value2")
>>> pipeline.get("key1")
>>> pipeline.get("key2")
>>> result = await client.exec(pipeline, false)
>>> print(result)
[OK, OK, b"value1", b"value2"]
Source code in glide_shared/commands/batch.py
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
class Batch(BaseBatch):
    """
    Batch implementation for standalone GlideClient. Batches allow the execution of a group
    of commands in a single step.

    Batch Response:
        An ``array`` of command responses is returned by the client ``exec`` command,
        in the order they were given. Each element in the array represents a command given to the Batch.
        The response for each command depends on the executed Valkey command.
        Specific response types are documented alongside each method.

    Args:
        is_atomic (bool): Determines whether the batch is atomic or non-atomic. If ``True``,
            the batch will be executed as an atomic transaction. If ``False``,
            the batch will be executed as a non-atomic pipeline.

    See [Valkey Transactions (Atomic Batches)](https://valkey.io/topics/transactions/) and [Valkey Pipelines (Non-Atomic Batches)](https://valkey.io/topics/pipelining/) for details.

    Note for Standalone Mode (Cluster Mode Disabled):
        Standalone Batches are executed on the primary node.

    Examples:
        ### Atomic Batch - Transaction:
        >>> transaction = Batch(is_atomic=True)  # Atomic (Transaction)
        >>> transaction.set("key", "value")
        >>> transaction.get("key")
        >>> result = await client.exec(transaction, false)
        >>> print(result)
        [OK, b"value"]

        #### Non-Atomic Batch - Pipeline:
        >>> pipeline = Batch(is_atomic=False)  # Non-Atomic (Pipeline)
        >>> pipeline.set("key1", "value1")
        >>> pipeline.set("key2", "value2")
        >>> pipeline.get("key1")
        >>> pipeline.get("key2")
        >>> result = await client.exec(pipeline, false)
        >>> print(result)
        [OK, OK, b"value1", b"value2"]

    """

    # TODO: add SLAVEOF and all SENTINEL commands
    def move(self, key: TEncodable, db_index: int) -> "Batch":
        """
        Move `key` from the currently selected database to the database specified by `db_index`.

        See [valkey.io](https://valkey.io/commands/move/) for more details.

        Args:
            key (TEncodable): The key to move.
            db_index (int): The index of the database to move `key` to.

        Commands response:
            bool: True if `key` was moved.

            False if the `key` already exists in the destination database
            or does not exist in the source database.
        """
        return self.append_command(RequestType.Move, [key, str(db_index)])

    def select(self, index: int) -> "Batch":
        """
        Change the currently selected database.

        See [valkey.io](https://valkey.io/commands/select/) for details.

        Args:
            index (int): The index of the database to select.

        Command response:
            A simple OK response.
        """
        return self.append_command(RequestType.Select, [str(index)])

    def copy(
        self,
        source: TEncodable,
        destination: TEncodable,
        destinationDB: Optional[int] = None,
        replace: Optional[bool] = None,
    ) -> "Batch":
        """
        Copies the value stored at the `source` to the `destination` key. If `destinationDB`
        is specified, the value will be copied to the database specified by `destinationDB`,
        otherwise the current database will be used. When `replace` is True, removes the
        `destination` key first if it already exists, otherwise performs no action.

        See [valkey.io](https://valkey.io/commands/copy) for more details.

        Args:
            source (TEncodable): The key to the source value.
            destination (TEncodable): The key where the value should be copied to.
            destinationDB (Optional[int]): The alternative logical database index for the destination key.
            replace (Optional[bool]): If the destination key should be removed before copying the value to it.

        Command response:
            bool: True if the source was copied.

            Otherwise, return False.

        Since: Valkey version 6.2.0.
        """
        args = [source, destination]
        if destinationDB is not None:
            args.extend(["DB", str(destinationDB)])
        if replace is not None:
            args.append("REPLACE")

        return self.append_command(RequestType.Copy, args)

    def publish(self, message: TEncodable, channel: TEncodable) -> "Batch":
        """
        Publish a message on pubsub channel.

        See [valkey.io](https://valkey.io/commands/publish) for more details.

        Args:
            message (TEncodable): Message to publish
            channel (TEncodable): Channel to publish the message on.

        Command Respose:
            int: Number of subscriptions in that shard that received the message.

        """
        return self.append_command(RequestType.Publish, [channel, message])

copy(source, destination, destinationDB=None, replace=None)

Copies the value stored at the source to the destination key. If destinationDB is specified, the value will be copied to the database specified by destinationDB, otherwise the current database will be used. When replace is True, removes the destination key first if it already exists, otherwise performs no action.

See valkey.io for more details.

Parameters:

Name Type Description Default
source TEncodable

The key to the source value.

required
destination TEncodable

The key where the value should be copied to.

required
destinationDB Optional[int]

The alternative logical database index for the destination key.

None
replace Optional[bool]

If the destination key should be removed before copying the value to it.

None
Command response

bool: True if the source was copied.

Otherwise, return False.

Since: Valkey version 6.2.0.

Source code in glide_shared/commands/batch.py
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
def copy(
    self,
    source: TEncodable,
    destination: TEncodable,
    destinationDB: Optional[int] = None,
    replace: Optional[bool] = None,
) -> "Batch":
    """
    Copies the value stored at the `source` to the `destination` key. If `destinationDB`
    is specified, the value will be copied to the database specified by `destinationDB`,
    otherwise the current database will be used. When `replace` is True, removes the
    `destination` key first if it already exists, otherwise performs no action.

    See [valkey.io](https://valkey.io/commands/copy) for more details.

    Args:
        source (TEncodable): The key to the source value.
        destination (TEncodable): The key where the value should be copied to.
        destinationDB (Optional[int]): The alternative logical database index for the destination key.
        replace (Optional[bool]): If the destination key should be removed before copying the value to it.

    Command response:
        bool: True if the source was copied.

        Otherwise, return False.

    Since: Valkey version 6.2.0.
    """
    args = [source, destination]
    if destinationDB is not None:
        args.extend(["DB", str(destinationDB)])
    if replace is not None:
        args.append("REPLACE")

    return self.append_command(RequestType.Copy, args)

move(key, db_index)

Move key from the currently selected database to the database specified by db_index.

See valkey.io for more details.

Parameters:

Name Type Description Default
key TEncodable

The key to move.

required
db_index int

The index of the database to move key to.

required
Commands response

bool: True if key was moved.

False if the key already exists in the destination database or does not exist in the source database.

Source code in glide_shared/commands/batch.py
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
def move(self, key: TEncodable, db_index: int) -> "Batch":
    """
    Move `key` from the currently selected database to the database specified by `db_index`.

    See [valkey.io](https://valkey.io/commands/move/) for more details.

    Args:
        key (TEncodable): The key to move.
        db_index (int): The index of the database to move `key` to.

    Commands response:
        bool: True if `key` was moved.

        False if the `key` already exists in the destination database
        or does not exist in the source database.
    """
    return self.append_command(RequestType.Move, [key, str(db_index)])

publish(message, channel)

Publish a message on pubsub channel.

See valkey.io for more details.

Parameters:

Name Type Description Default
message TEncodable

Message to publish

required
channel TEncodable

Channel to publish the message on.

required
Command Respose

int: Number of subscriptions in that shard that received the message.

Source code in glide_shared/commands/batch.py
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
def publish(self, message: TEncodable, channel: TEncodable) -> "Batch":
    """
    Publish a message on pubsub channel.

    See [valkey.io](https://valkey.io/commands/publish) for more details.

    Args:
        message (TEncodable): Message to publish
        channel (TEncodable): Channel to publish the message on.

    Command Respose:
        int: Number of subscriptions in that shard that received the message.

    """
    return self.append_command(RequestType.Publish, [channel, message])

select(index)

Change the currently selected database.

See valkey.io for details.

Parameters:

Name Type Description Default
index int

The index of the database to select.

required
Command response

A simple OK response.

Source code in glide_shared/commands/batch.py
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
def select(self, index: int) -> "Batch":
    """
    Change the currently selected database.

    See [valkey.io](https://valkey.io/commands/select/) for details.

    Args:
        index (int): The index of the database to select.

    Command response:
        A simple OK response.
    """
    return self.append_command(RequestType.Select, [str(index)])