Threading and concurrency
Clone this wiki locally
In general, you should think about IO strategies for tornado apps in this order:
1) Use an async library if available (e.g.
AsyncHTTPClient instead of
2) Make it so fast you don't mind doing it synchronously and blocking the
IOLoop. This is most appropriate for things like memcache and database queries that are under your control and should always be fast. If it's not fast, make it fast by adding the appropriate indexes to the database, etc.
3) Do the work in a
ThreadPoolExecutor. Remember that worker threads cannot access the
IOLoop (even indirectly) so you must return to the main thread before writing any responses.
4) Move the work out of the tornado process. If you're sending email, for example, just write it to the database and let another process (whose latency doesn't matter) read from the queue and do the actual sending.
5) Block the
IOLoop anyway. This is the lazy way out but may be acceptable in some cases.