-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Threading and concurrency
In general, you should think about IO strategies for tornado apps in this order:
-
Use an async library if available (e.g.
AsyncHTTPClient
instead ofrequests
). -
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. -
Do the work in a
ThreadPoolExecutor
. Remember that worker threads cannot access theIOLoop
(even indirectly) so you must return to the main thread before writing any responses. -
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.
-
Block the
IOLoop
anyway. This is the lazy way out but may be acceptable in some cases.