Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tornado Asynchronous Handler

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.

For example:

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 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?

like image 234
Jarrod Avatar asked Feb 08 '11 21:02

Jarrod


People also ask

Is Tornado asynchronous?

Tornado is a Python web framework and asynchronous networking library, originally developed at FriendFeed.

Does Tornado use Asyncio?

New in version 3.2. This module integrates Tornado with the asyncio module introduced in Python 3.4. This makes it possible to combine the two libraries on the same event loop.

Is Tornado multithreaded?

The reason is that Tornado is an asynchronous server with only one thread.

Is Tornado single threaded?

To minimize the cost of concurrent connections, Tornado uses a single-threaded event loop. This means that all application code should aim to be asynchronous and non-blocking because only one operation can be active at a time.


2 Answers

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

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/wiki/Threading-and-concurrency

like image 155
Schildmeijer Avatar answered Sep 28 '22 02:09

Schildmeijer


How about having get_current_user return a Future that you signal when the asynchronous response from your database is returned?

class BaseHandler(tornado.web.RequestHandler):
    def get_current_user(self):
        future = Future()
        def query_cb(user):
            future.set_result(user or None)
        database.get(username='test', password='t3st', callback=query_cb)
        return future


class MainHandler(BaseHandler):
    @gen.coroutine
    def get(self):
        user = yield self.get_current_user()
        self.write('user: ' + user)
        # ... actual request processing
like image 38
Myk Willis Avatar answered Sep 28 '22 01:09

Myk Willis