Make sure you are closing the DB connections after accessing Django ORM in your threads

By | April 22, 2016

I was writing a service which import various feeds and then post to another RESTful API, simple enough as below:

Then the feed model:

Then run this feed in trivial code:

And you will quickly notice the threads complaining

What it's happening is Django will create a new connection per thread when you access the ORM, and connections are left open even the threads are terminated, in this case the culprit is

So in this case, simply refactor the code to close the connection before return:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.