Created
February 11, 2011 14:30
-
-
Save anonymous/822419 to your computer and use it in GitHub Desktop.
Clean up redis connections after the termination of each request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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