Using redislite with existing python code

The redislite patch functionality can be used to make many existing python modules that use redis to work with minimal modifications.



from redislite import Redis

# Create a Redis instance using redislite
REDIS_DB_PATH = os.path.join('/tmp/my_redis.db')
rdb = Redis(REDIS_DB_PATH)
REDIS_SOCKET_PATH = 'redis+socket://%s' % (rdb.socket_file, )

# Use redislite for the Celery broker

# (Optionally) use redislite for the Celery result backend

from celery import Celery

# for django projects
from django.conf import settings
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings')
celery_app = Celery('my_app')

# for other projects
celery_app = Celery('my_app')

Note that only one Redis instance is created, and others merely discover the running instance and use its existing socket file path.


Use a redisliste.StrictRedis() object for the redis-collections redis keyword argument to use it with redislite. If the redis-collection variable needs to be persistent make sure to pass the redislite.StrictRedis() class a dbfilename argument to use.

>>> import redislite
>>> import redis_collections
>>> example_dict = redis_collections.Dict(redis=redislite.StrictRedis('example.rdb'))
>>> example_dict['test'] = 'This is a test variable'
>>> example_dict
<redis_collections.Dict at 7908fc2cc97d49fda4bce7365df3b373 {'test': 'This is a test variable'}>


When using rq you will need to specify a db_filename for the connection.

To put jobs on queues, you don’t have to do anything special, just define your typically lengthy or blocking function:

import requests

def count_words_at_url(url):
    resp = requests.get(url)
    return len(resp.text.split())

Then, create a RQ queue:

from redislite import Redis
from rq import Queue

q = Queue(connection=Redis('RQ_example.rdb'))

And enqueue the function call:

from my_module import count_words_at_url
result = q.enqueue(
             count_words_at_url, '')

For a more complete example, refer to the RQ docs. To start executing enqueued function calls in the background, start a worker from your project’s directory:

$ rqworker
*** Listening for work on default
Got count_words_at_url('') from default
Job result = 818
*** Listening for work on default


First, install both walrus and redislite.

Install both modules:

$ pip install walrus redislite

Then patch redis before using walrus. Optionally specifying a redis db if the result needs to be usable after the script finishes running.

>>> from redislite.patch import patch_redis
>>> patch_redis('/tmp/walrus.db')
>>> from walrus import *
>>> db = Database()
>>> huey = db.Hash('huey')
>>> huey.update(color='white', temperament='ornery', type='kitty')
<Hash "huey": {'color': 'white', 'type': 'kitty', 'temperament': 'ornery'}>
>>> huey.keys()
['color', 'type', 'temperament']
>>> 'color' in huey
>>> huey['color']