Using Redis Python (Redis Py)
In order to use Redis with Python you will need a Python Redis client. In following sections, we will demonstrate the use of redis-py, a Redis Python Client. Additional Python clients for Redis can be found under the Python section of the Redis Clients page.
redis-py’s installation instructions are given in the “Installation” section of its README file. This is the easiest way to install python redis. Use the Redis Python
pip to install redis-py:
$ sudo pip install redis
You can also download the latest redis-py release from the GitHub repository to understand Python Redis, Python Lab, & Python database updates. To install Redis Python, extract the source and run the following command:
$ cd redis-py ~/redis-py$ sudo python setup.py install
Opening a Connection to Redis Using redis-py
The following code creates a connection to Redis using redis-py:
import redis r = redis.Redis( host='hostname', port=port, password='password')
To adapt this example to your code, make sure that you replace the following values with those of your database:
- In line 4,
hostshould be set to your database’s hostname or IP address
- In line 5,
portshould be set to your database’s port
- In line 6,
passwordshould be set to your database’s password
Connection Pooling with redis-py
redis-py provides a connection pooling mechanism as explained in the Connection Pools section of its README file. Since connection pooling is enabled by default, no special actions are required to use it. This is a substantial benefit of Python Redis.
Using SSL and redis-py
redis-py is the second Redis Python client that natively supported SSL. Use the
SSLConnection class or simply instantiate your connection pool using a
rediss://-URL and the
from_url method, like so:
r = redis.Redis( url='rediss://:password@hostname:port/0', password='password', ssl_keyfile='path_to_keyfile', ssl_certfile='path_to_certfile', ssl_cert_reqs='required', ssl_ca_certs='path_to_ca_certfile')
Reading and Writing Data with redis-py
Once connected to Redis, you can start reading and writing data with Python Redis. The following code snippet writes the value
bar to the Redis key
foo, reads it back, and prints it:
# open a connection to Redis ... r.set('foo', 'bar') value = r.get('foo') print(value)
The output of the above code should be:
$ python example_redis-py.py bar
Redis Enterprise enables running Redis Python datasets & Python Redis client in a highly available and auto-scalable manner, with predictable top performance.
The Redis Enterprise Software lets you install an enterprise grade Redis Python cluster in your environment of choice, whether an on-premises (on-prem) data-center or your preferred Redis cloud platform. It gives you full control of your data and configuration – no clustering or sharding knowledge required!
Redis Enterprise Cloud is a fully-managed cloud service for hosting and running Redis dataset without dealing with nodes, clusters, failure recovery or performance stabilization. Our technology does all that in a fully automated manner. Redis Enterprise Cloud through Redis Labs is available on all popular clouds and platforms-as-a-service.
For more information on using Redis Labs’ products and services with Redis Python please see the Howto page.