Skip to content

Instantly share code, notes, and snippets.

Created February 11, 2011 14:30
Show Gist options
  • Save anonymous/822419 to your computer and use it in GitHub Desktop.
Save anonymous/822419 to your computer and use it in GitHub Desktop.
Clean up redis connections after the termination of each request
from django.conf import settings
from django.core.signals import request_finished
from redis.client import Redis
database = Redis(
host=getattr(settings, "REDIS_HOST", "localhost"),
port=getattr(settings, "REDIS_PORT", 6379),
db=getattr(settings, "REDIS_DB", 0),
password=getattr(settings, "REDIS_PASSWORD", ""))
def cleanup(sender, **kwargs):
try:
database.connection.disconnect()
except (KeyboardInterrupt, SystemExit, MemoryError):
raise
except:
pass
# Clean up the connection on every request
# There's no need to have re-connection logic because Redis handles that internally
request_finished.connect(cleanup)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment