title: Using Django ORM in threads and avoiding "too many clients" exception by using BoundedSemaphore
body: <p>I work on manage.py command which creates about 200 threads to check remote hosts. My database setup allows me to use 120 connections, so I need to use some kind of pooling. I've tried using separated thread, like this</p>
  
  <pre><code>class Pool(Thread):
      def __init__(self):
          Thread.__init__(self)        
          self.semaphore = threading.BoundedSemaphore(10)
  
      def give(self, trackers):
          self.semaphore.acquire()
          data = ... some ORM (not lazy, query triggered here) ...
          self.semaphore.release()
          return data
  </code></pre>
  
  <p>I pass instance of this object to every check-thread but still getting "OperationalError: FATAL:  sorry, too many clients already" inside Pool object after init-ing 120 threads .
  I've expected that only 10 database connections will be opened and threads will wait for free semaphore slot. I can check that semaphore works by commenting "release()", in that case only 10 threads will work and other will wait till app termination. </p>
  
  <p>As much as I understand, every thread is opening new connection to database even if actual call is inside different thread, but why? Is there any way to perform all database queries inside only one thread?</p>