龙卷风异步处理程序 [英] Tornado Asynchronous Handler

查看:127
本文介绍了龙卷风异步处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实现在RequestHandler龙卷风get_current_user,但我需要打电话时等待的异步调用到我的数据库来阻止。装饰用@ tornado.web.asynchronous通话,因为无论方式方法get_current_user异步查询之前返回并完成执行查询回调将无法工作。

I am attempting to implement get_current_user in the RequestHandler for Tornado, but I need the call to block while waiting on the asynchronous call to my database. Decorating the call with @tornado.web.asynchronous will not work because either way the get_current_user method returns before the async query completes and the query callback is executed.

例如:

class MyHandler(BaseHandler):
  @tornado.web.asynchronous
  @tornado.web.authenticated
  def get(self):
    self.write('example')
    self.finish()

class BaseHandler(tornado.web.RequestHandler):
  def get_current_user(self):
    def query_cb(self, doc):
      return doc or None

    database.get(username='test', password='t3st', callback=query_cb)

@ tornado.web.authenticated调用get_current_user,但始终秉承无,因为BaseHandler没有时间作出反应。有没有一种方法,使用龙卷风,为一个呼叫暂时阻止如上面的那个?

@tornado.web.authenticated calls get_current_user, but always receives "None" because the BaseHandler does not have time to respond. Is there a way, using tornado, to temporarily block for a call such as the one above?

推荐答案

做一个阻止数据库操作,而不是上述的无阻塞(有阻塞的mysql的lib随龙卷风)。

Do a blocking database operation instead of the non blocking described above (There is a blocking mysql lib shipped with tornado).

从对线程和并发性的龙卷风维基页面:
同步这样做,可以阻止IOLoop,这是最适合这样的事情是你的控制之下,并应始终快速内存缓存和数据库查询。如果不是很快,使其快速通过添加相应的索引数据库,等等。

From the Tornado wiki page about threads and concurrency: "Do it synchronously and block 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."

https://github.com/facebook/tornado/wi​​ki/Threading-和并发

这篇关于龙卷风异步处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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