From 9de85dc0817419d9fdefe689521950882654ef50 Mon Sep 17 00:00:00 2001 From: "A. Jesse Jiryu Davis" Date: Thu, 29 Mar 2012 19:12:47 -0400 Subject: [PATCH] Recommendations for using Tornado with PyMongo --- doc/faq.rst | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/doc/faq.rst b/doc/faq.rst index 87b9b2e32..bdbe33446 100644 --- a/doc/faq.rst +++ b/doc/faq.rst @@ -49,6 +49,27 @@ There are asynchronous MongoDB drivers in Python: `AsyncMongo for Tornado however, these projects are less stable, lack features, and are less actively maintained. +It is possible to use PyMongo with Tornado, if some precautions are taken to +avoid blocking the event loop: + +- Make sure all MongoDB operations are very fast. Use the + `MongoDB profiler `_ + to watch for slow queries. + +- Create a single :class:`~pymongo.connection.Connection` instance for your + application in your startup code, before starting the IOLoop. + +- Configure the :class:`~pymongo.connection.Connection` with a short + ``socketTimeoutMS`` so slow operations result in a + :class:`~pymongo.errors.TimeoutError`, rather than blocking the loop and + preventing your application from responding to other requests. + +- Start up extra Tornado processes. Tornado is typically deployed with one + process per CPU core, proxied behind a load-balancer such as + `Nginx `_ or `HAProxy `_; + when using Tornado with a blocking driver like PyMongo it's recommended you + start two or three processes per core instead of one. + What does *OperationFailure* cursor id not valid at server mean? ---------------------------------------------------------------- Cursors in MongoDB can timeout on the server if they've been open for