title: SQLAlchemy/Python: Using an engine per process, and injecting that engine into dependencies
body: <p>SQLAlchemy recommends using one engine per process because the underlying DBAPI connections are not necessarily safe to use across processes.</p>
  
  <p>I have global <code>engine</code> and <code>session</code> variables that gets lazily populated with a SQLAlchemy engine and a session factory, respectively. This makes it easy to write database helpers that depend can call <code>get_session()</code> at runtime to use the <code>session</code> variable after it has been assigned a value by <code>init_db</code>.</p>
  
  <p>That works well for letting helpers that need access to <code>engine</code> or <code>session</code> be imported before <code>engine</code> or <code>session</code> are initialized. But this pattern locks me into using one <code>engine</code> and <code>session</code> globally.</p>
  
  <p>However, what I really need to do is:</p>
  
  <pre><code>engine, session = init_db(DB_URL)
  </code></pre>
  
  <p>... from the calling application/method, and then inject the session into these database helpers. Why? Well, I need to use one engine per process. I think I need an inversion of control pattern here, but am having trouble coming up with something workable (that doesn't involve passing the session around to the helpers on each call, which is hacky and a last resort).</p>
  
  <p>I thought I could just set up the engine after <code>os.fork()</code>. Each process would start with <code>engine=None</code>, then instantiate it. But <code>engine</code> has the same memory address in both the parent and child process. This makes sense; it's how Python works.</p>
  
  <pre><code>from utils import init_db, engine, DB_URL
  import os
  
  def connect():
      print('before fork, engine is: {}'.format(id(engine)))
      newpid = os.fork()
  
      if newpid == 0:
          init_db(DB_URL)
          print('child engine is: {}'.format(id(engine))) # id(engine) is equal to below call
      else:
          init_db(DB_URL)
          print('parent engine is: {}'.format(id(engine))) # id(engine) is equal to above call
  
  if __name__ == '__main__':
      connect()
  </code></pre>
  
  <p>Is my best option to create a pool of sorts based on the process ID? I'd like to use a clean inversion of control pattern if possible.</p>
  
  <p><code>NullPool</code> doesn't pool connections and would make this work, but I don't think that's a good thing - it seems expensive to constantly open and close connections?</p>
  
  <p>Python version: 3.4.0</p>
  
  <p>SQLAlchemy version: 0.9.7</p>