【问题标题】:Is this right way to call coroutine method in Tornado framework?这是在 Tornado 框架中调用协程方法的正确方法吗?
【发布时间】:2015-03-16 21:39:23
【问题描述】:

我的 Tornado 应用程序中有 WebSocketHandler。 我不确定这是使代码异步的正确方法。

class MyHandler(WebSocketHandler):
    def open(self):
        do something ...
        self.my_coroutine_method()

    @gen.coroutine
    def my_coroutine_method(self):
        user = yield db.user.find_one() # call motor asynchronous engine
        self.write_message(user)

【问题讨论】:

    标签: python tornado tornado-motor


    【解决方案1】:

    是的,这是正确的。但是,在某些情况下,简单地调用协程而不使用 yield 可能会导致以意想不到的方式处理异常,因此我建议在像这样从非协程调用协程时使用 IOLoop.current().spawn_callback(self.my_coroutine_method)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-10
      • 1970-01-01
      • 2010-11-02
      • 1970-01-01
      • 2015-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多