1 回答
慕神8447489
TA贡献1780条经验 获得超1个赞
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 requests).
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.
1) Use an async library if available (e.g. AsyncHTTPClient instead of requests).
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.
- 1 回答
- 0 关注
- 754 浏览
添加回答
举报
0/150
提交
取消