title: Why does Elixir/SQLAlchemy's session.bind get set to None within threads?
body: <p>I'll start with some simplified test code to demonstrate the issue I'm referencing.</p>
  
  <p>t_model.py</p>
  
  <pre><code>from elixir import *
  
  metadata.bind = 'sqlite:///test.db'
  session.bind = metadata.bind
  </code></pre>
  
  <p>t_main.py</p>
  
  <pre><code>#!/usr/bin/python  
  import t_model 
  import threading 
  
  class TestThread(threading.Thread): 
      def run(self): 
          print 'Thread ID: %s, t_model.session.bind = %s' % (threading.current_thread(), t_model.session.bind) 
  
  class Test(object): 
      def run(self): 
          print 'Thread ID: %s, t_model.session.bind = %s' % (threading.current_thread(), t_model.session.bind) 
  
  if __name__ == "__main__": 
      Test().run() 
      TestThread().run() 
      TestThread().start() 
  </code></pre>
  
  <p>The output of t_main.py:</p>
  
  <pre><code>Thread ID: &lt;_MainThread(MainThread, started 140111907010336)&gt;, t_model.session.bind = Engine(sqlite:///test.db)
  Thread ID: &lt;_MainThread(MainThread, started 140111907010336)&gt;, t_model.session.bind = Engine(sqlite:///test.db)
  Thread ID: &lt;TestThread(Thread-2, started 140111865108224)&gt;, t_model.session.bind = None
  </code></pre>
  
  <p>I realize that there is some under the hood work being done by Elixir/SQLAlc to instantiate new sessions for threads (I presume).  I believe that is what Ants Aasma's response to <a href="https://stackoverflow.com/questions/1353131/issues-with-scoped-session-in-sqlalchemy-how-does-it-work">this question</a> addresses.  However, why does it not carry over fields set like that?  Is there a way to ensure something like that carries over?</p>
  
  <p>I need the engine bound to the session so that I can make certain raw sql queries, but as soon as I instantiate a thread it forgets the value.</p>
  
  <p>Any insight you guys can provide to help me understand better the legwork going on and how I might avoid having to set t_model.session.bind every time I initialize a new thread and set the engine?</p>