Valkey: Connecting Python to Valkey
This documentation is part of the Using with Python guide. You can view the complete guide here: How to connect Valkey with Python.
👋 Welcome to the Stackhero documentation!
Stackhero offers a ready-to-use Valkey cloud solution that provides a host of benefits, including:
Redis Commanderweb UI included.- Unlimited message size and transfers.
- Effortless updates with just a click.
- Optimal performance and robust security powered by a private and dedicated VM.
Save time and simplify your life: it only takes 5 minutes to try Stackhero's Valkey cloud hosting solution!
Let us explore how you can connect your Python application to Valkey. Here is a straightforward example using default settings:
import redis
r = redis.from_url(
'rediss://default:<password>@<XXXXXX>.stackhero-network.com:<PORT_TLS>',
health_check_interval=10,
socket_connect_timeout=5,
retry_on_timeout=True,
socket_keepalive=True
)
For enhanced security, it is advised to manage credentials using environment variables. Here is how you can do that:
import os
import redis
r = redis.from_url(
os.environ.get("STACKHERO_VALKEY_URL_TLS"),
health_check_interval=10,
socket_connect_timeout=5,
retry_on_timeout=True,
socket_keepalive=True
)
Ensure your environment variables include an entry like this: STACKHERO_VALKEY_URL_TLS=rediss://default:<password>@<XXXXXX>.stackhero-network.com:<PORT_TLS>.