Home

Tags

Example: inline-callback asyncmongo in Tornado

2011-09-21 tornado mongodb asyncmongo inline_callback

Пример использования inline-callback при использовании asyncmongo, tornado 2.0

# coding:utf8

import tornado.ioloop
from tornado import web, gen
import asyncmongo

class MainHandler(tornado.web.RequestHandler):
    @property
    def db(self):
        if not hasattr(self, '_db'):
            self._db = asyncmongo.Client(pool_id='mydb', host='127.0.0.1', \
                port=27017, maxcached=10, maxconnections=50, dbname='test')
        return self._db

    @web.asynchronous
    @gen.engine
    def get(self):
        r, error = yield gen.Task(self.db.user.save, { 'login':'tester' })

        r, error = yield gen.Task(self.db.user.find_one, {})
        self.write(str(r[0]))

        self.finish()

application = tornado.web.Application([
    (r"/", MainHandler),
], debug=True)

if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

comments powered by Disqus