I'm have a Django micro-service, that constantly runs a lot of tasks in the background (using a few child processes that run multiple threads), regardless of the request-response flow.
Since every thread uses the ORM constantly, at some point I get an error for "too many clients" connected to the DB, and after that happens, it persists even after restarting the worker that got the exception.
This is weird, since the app never spawns more than around 30-40 threads in parallel, and the PostgreSQL DB supports 100, but I guess every thread makes Django create a few connections of something like that...
I've tried using db.connection.close() at the end of each thread, but it seems to have no effect.
Is there any way to make Django use a single connection cursor for each process? Why does it even create new connections for each thread/query/whatever?
NOTE:
I am not sharing the connection between the worker processes. Before spawning the child processes, I used db.connections.close_all()