title: across process boundary in scoped_session
body: <p>I'm using SQLAlchemy and multiprocessing. I also use scoped_session sinse it avoids share the same session but I've found an error and their solution but  I don't understand why does it happend.</p>
  
  <p>You can see my code below:</p>
  
  <p><strong>db.py</strong></p>
  
  <pre><code>engine = create_engine(connection_string)
  
  Session = sessionmaker(bind=engine)
  DBSession = scoped_session(Session)
  </code></pre>
  
  <p><strong>script.py</strong></p>
  
  <pre><code>from multiprocessing import Pool, current_process
  from db import DBSession
  
  def process_feed(test):
      session = DBSession()
      print(current_process().name, session)
  
  def run():
      session = DBSession()
      pool = Pool()
      print(current_process().name, session)
      pool.map_async(process_feed, [1, 2]).get()
  
  if __name__ == "__main__":
      run()
  </code></pre>
  
  <p>When I run <code>script.py</code> The output is:</p>
  
  <pre><code>MainProcess &lt;sqlalchemy.orm.session.Session object at 0xb707b14c&gt;
  ForkPoolWorker-1 &lt;sqlalchemy.orm.session.Session object at 0xb707b14c&gt;
  ForkPoolWorker-2 &lt;sqlalchemy.orm.session.Session object at 0xb707b14c&gt;
  </code></pre>
  
  <p>Note that session object is the same <code>0xb707b14c</code> in the main process and their workers (child process)</p>
  
  <p>BUT If I change the order of first two lines run():</p>
  
  <pre><code>def run():
      pool = Pool() # &lt;--- Now pool is instanced in the first line
      session = DBSession()  # &lt;--- Now session is instanced in the second line
      print(current_process().name, session)
      pool.map_async(process_feed, [1, 2]).get()
  </code></pre>
  
  <p>And the I run <code>script.py</code> again the output is:</p>
  
  <pre><code>MainProcess &lt;sqlalchemy.orm.session.Session object at 0xb66907cc&gt;
  ForkPoolWorker-1 &lt;sqlalchemy.orm.session.Session object at 0xb669046c&gt;
  ForkPoolWorker-2 &lt;sqlalchemy.orm.session.Session object at 0xb66905ec&gt;
  </code></pre>
  
  <p>Now the session instances are different. </p>