SHARE
TWEET

Tornado nested callback example

a guest Jun 21st, 2012 56 Never
  1. from tornado import gen
  2. import tornado.web
  3.  
  4. class MainHandler(tornado.web.RequestHandler):
  5.     @tornado.web.asynchronous
  6.     @gen.engine
  7.     def get(self):
  8.         cool_result = yield gen.Task(self.nested, 2)
  9.         self.write(cool_result)
  10.  
  11.     @gen.engine
  12.     def nested(self, result_outer, callback = {}):
  13.         result = yield gen.Task(self.asum, [1,result_outer])
  14.         callback(result)
  15.  
  16.     def asum(self, l, callback={}):
  17.         r = sum(l)
  18.         return callback(r)
RAW Paste Data
Top