Valkey GLIDE Async client

2.3.1 · active · verified Mon Apr 13

Valkey GLIDE (General Language Independent Driver for the Enterprise) is an official open-source client library for Valkey and Redis OSS. Built with a Rust core and language-specific wrappers, it offers high-performance, reliability, and consistency across multiple languages. The Python client, currently at version 2.3.1, provides both asynchronous and synchronous interfaces and is regularly updated to support new Valkey features and best practices.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates connecting to a standalone Valkey instance and a Valkey cluster using `valkey-glide`'s asynchronous clients. It performs a `PING` command to verify connection and `SET`/`GET` operations. For cluster mode, ensure `VALKEY_CLUSTER_HOST` and `VALKEY_CLUSTER_PORT` environment variables are set to your cluster's entry point, or adjust the default `localhost:7000` to a running cluster node.

import asyncio
from glide import GlideClient, GlideClusterClient, NodeAddress
import os

async def main():
    # Example for a standalone Valkey instance
    # Ensure a Valkey server is running on localhost:6379 (e.g., via Docker)
    # docker run -d --name valkey-server -p 6379:6379 valkey/valkey:latest
    print("\n--- Standalone Client Example ---")
    standalone_client = GlideClient([NodeAddress("localhost", 6379)])
    try:
        pong_response = await standalone_client.ping()
        print(f"Standalone PING response: {pong_response}")

        await standalone_client.set("mykey", "myvalue")
        value = await standalone_client.get("mykey")
        print(f"Standalone GET mykey: {value}")
    except Exception as e:
        print(f"Error with Standalone Client: {e}")
    finally:
        await standalone_client.close()

    # Example for a Valkey cluster instance
    # Replace with actual cluster addresses
    # For local testing, you might need a local Valkey cluster setup
    print("\n--- Cluster Client Example ---")
    cluster_addresses = [NodeAddress(os.environ.get('VALKEY_CLUSTER_HOST', 'localhost'), int(os.environ.get('VALKEY_CLUSTER_PORT', '7000')))]
    cluster_client = GlideClusterClient(cluster_addresses)
    try:
        cluster_pong_response = await cluster_client.ping()
        print(f"Cluster PING response: {cluster_pong_response}")

        await cluster_client.set("cluster_key", "cluster_value")
        cluster_value = await cluster_client.get("cluster_key")
        print(f"Cluster GET cluster_key: {cluster_value}")
    except Exception as e:
        print(f"Error with Cluster Client: {e}")
    finally:
        await cluster_client.close()

if __name__ == "__main__":
    asyncio.run(main())

view raw JSON →