python-2.7之Python Tornado I/O 循环 current() 与 instance() 方法

kevingrace 阅读:135 2025-06-02 22:19:02 评论:0

我一直在查看 Tornado 文档。

在阅读 IOLoop 时,有如下内容。

In general you should use IOLoop.current as the default when constructing an asynchronous object, and use IOLoop.instance when you mean to communicate to the main thread from a different one.

我必须使用 instance() 方法通过共享一个全局 IOLoop 实例的多线程进行通信是有道理的。

但是这里的异步对象是什么,在异步对象的情况下为什么要使用current()

请您参考如下方法:

“异步对象”只是指像IOStreamHTTPServer 这样具有异步方法的对象。

您应该几乎总是使用IOLoop.current() 而不是IOLoop.instance()。在大多数情况下,它们是等价的,因为您只有一个线程和一个 IOLoop,但是当它们不同时,current() 通常就是您想要的。唯一应该使用 IOLoop.instance() 的情况是 A) 你有多个线程和 B) IOLoop 线程以外的线程需要调用 add_callback(这是另一个线程可以与 IOLoop 交互的唯一方式。


标签:Python
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号