title: What are the Tornado and Mongodb blocking and asynchronous considerations?
body: <p>I am running the Tornado web server in conjunction with Mongodb (using the pymongo driver).  I am trying to make architectural decisions to maximize performance.</p>
  
  <p>I have several subquestions regarding the blocking/non-blocking and asynchronous aspects of the resulting application when using Tornado and pymongo together:</p>
  
  <h2>Question 1:  Connection Pools</h2>
  
  <p>It appears that the <code>pymongo.mongo_client.MongoClient</code> object automatically implements a pool of connections.  Is the intended purpose of a "connection pool" so that I can access mongodb simultaneously from different threads?  Is it true that if run with a single MongoClient instance from a single thread that there is really no "pool" since there would only be one connection open at any time?</p>
  
  <h2>Question 2: Multi-threaded Mongo Calls</h2>
  
  <p>The following FAQ:</p>
  
  <p><a href="http://api.mongodb.org/python/current/faq.html#does-pymongo-support-asynchronous-frameworks-like-gevent-tornado-or-twisted" rel="nofollow noreferrer">http://api.mongodb.org/python/current/faq.html#does-pymongo-support-asynchronous-frameworks-like-gevent-tornado-or-twisted</a></p>
  
  <p>states:</p>
  
  <blockquote>
    <p>Currently there is no great way to use PyMongo in conjunction with
    Tornado or Twisted. PyMongo provides built-in connection pooling, so
    some of the benefits of those frameworks can be achieved just by
    writing multi-threaded code that shares a MongoClient.</p>
  </blockquote>
  
  <p>So I assume that I just pass a single <code>MongoClient</code> reference to each thread?  Or is there more to it than that?  What is the best way to trigger a callback when each thread produces a result?  Should I have one thread running who's job it is to watch a queue (python's <code>Queue.Queue</code>) to handle each result and then calling <code>finish()</code> on the left open <code>RequestHandler</code> object in Tornado? (of course using the <code>tornado.web.asynchronous</code> decorator would be needed)</p>
  
  <h2>Question 3: Multiple Instances</h2>
  
  <p>Finally, is it possible that I am just creating work?  Should I just shortcut things by running a single threaded instance of Tornado and then start 3-4 instances per core?  (The above FAQ reference seems to suggest this)</p>
  
  <p>After all doesn't the GIL in python result in effectively different processes anyway?  Or are there additional performance considerations (plus or minus) by the "non-blocking" aspects of Tornado?  (I know that this is non-blocking in terms of I/O as pointed out here:  <a href="https://stackoverflow.com/questions/3638844/is-tornado-really-non-blocking">Is Tornado really non-blocking?</a>)</p>
  
  <p>(Additional Note: I am aware of asyncmongo at:  <a href="https://github.com/bitly/asyncmongo" rel="nofollow noreferrer">https://github.com/bitly/asyncmongo</a>  but want to use pymongo directly and not introduce this additional dependency.)</p>