为了账号安全,请及时绑定邮箱和手机立即绑定

如何理解 Tornado 官方关于并发的一条建议

如何理解 Tornado 官方关于并发的一条建议

泛舟湖上清波郎朗 2018-10-10 12:29:52
查看完整描述

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.
查看完整回答
反对 回复 2018-10-24
  • 1 回答
  • 0 关注
  • 725 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信