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

查看:157
本文介绍了如何理解 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: 最新

解决方案

Tornado 的绝大部分 API 都不是线程安全的,从其它线程操作会出(各种奇怪的)问题。只有 IOLoop.add_callback 这一个 API 是线程安全的:

It is safe to call this method from any thread at any time, except from a signal handler. Note that this is the only method in IOLoop that makes this thread-safety guarantee; all other interaction with the IOLoop must be done from that IOLoop‘s thread.

你使用 tornado.concurrent.run_on_executor 就可以了。它自动帮你完成扔任务到线程池、得到结果时取回的逻辑。

这篇关于如何理解 Tornado 官方关于并发的一条建议 ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆