Hello. I have a standalone script that is long-running (i.e is supposed to run in a loop forever). This script uses the Django ORM for work on a database. For example, every two seconds it does a MyModel.objects.all().
Works awesome, but after a while I get OperationalError: FATAL: remaining connection slots are reserved for non-replication superuser connections The db in use is postgresql with psycopg2 backend. If I watch the number of connections in psql I see the number go up and up and up. So it's like the above query creates a NEW connection every time through the loop. I read that in django connections get closed at the end of "request", but since this is a long running process with no request the signal that causes the connection to close doesn't ever get triggered and the connections never close. Ideally I think I would want to create a single connection for the process and keep it open forever (or at least a long time) and somehow "pass" that connection to the ORM to use, rather than opening a new connection for every query. There is also connection pooling http://www.craigkerstiens.com/2013/03/07/Fixing-django-db-connections/, but now that I got this error I remember that I had this same problem in the past with a long-running script that uses django orm. In that case I tried using connection pooling, but it didn't work. In the end I migrated all the db stuff to use pymongo and handled connections manually (which ended up working like a charm). I'd like to avoid this route this time. Any insight on how to handle this would be great. -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c853ab92-9389-4e65-b33e-2b82ce4eaca3%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.