title: Django long running scripts and DB CPU usage
body: <p>I running a script that uses Django ORM. The script runs for long time sometimes more than three days. Script looks like below</p>
  
  <pre><code>from app.models import *
  
  for i in some_simpe_select():
      more_simple_selects()
      processing_for_few_minutes()
      new_object.save()
  </code></pre>
  
  <p>There are multiple instances for same script running simultaneously with slightly different options. We use postgresql. Over the time this causes DB CPU to go high. ps shows postgres processes idle in transaction but CPU usage unusually high.</p>
  
  <p>I wonder what are best practices around long running django scripts, particularly around high db CPU usage. </p>
  
  <p>Django 1.4, Python 2.7, Postgresql 9.1</p>