如何理解 Tornado 官方关于并发的一条建议 ?
实际现象
tornado官方给出的一条建议
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.
大意为: 把阻塞的工作交给线程池, 不能去阻塞/干扰 IOLoop主循环
那么如何做到它所说的Remember that worker threads cannot access the IOLoop (even indirectly) so you must return to the main thread before writing any responses.
? ( 用示例代码佐证 )
相关代码
https://github.com/tornadoweb...
全文
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.
不懂的地方
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.
期望
利用Tornado写出高并发的应用( IO型 )
上下文环境
产品版本: Python 3.5
操作系统: Linux Server
Tornado: 最新
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Tornado 的绝大部分 API 都不是线程安全的,从其它线程操作会出(各种奇怪的)问题。只有
IOLoop.add_callback
这一个 API 是线程安全的:你使用
tornado.concurrent.run_on_executor
就可以了。它自动帮你完成扔任务到线程池、得到结果时取回的逻辑。