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

发布于 2022-09-04 18:24:49 字数 2041 浏览 15 评论 0

实际现象

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里的微风 2022-09-11 18:24:49

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 就可以了。它自动帮你完成扔任务到线程池、得到结果时取回的逻辑。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文