[Answered ]-Dynamic per-request database connections in Django


rereading the file is a heavy penalty to pay when it’s unlikely that the file has changed.

My usual approach is to use INotify to watch for configuration file changes, rather than trying to read a file on every request. Additionally, I tend to keep a “current” configuration, parsed from the file, and only replace it with a new value once i’ve finished parsing the config file and i’m certain it’s valid. You could resolve some of your concerns about thread safety by setting the current configuration on each incoming request, so that the configuration can’t change mid-way through a request.


You could start different instances with different settings.py files (by setting different DJANGO_SETTINGS_MODULE) on different ports, and redirect the requests to the specific apps. Just my 2 cents.

Leave a comment