一个tornado+coroutine+yield的例子

一个简单的tornado+coroutine+yield的例子

class MainHandler(tornado.web.RequestHandler):
    cond = None

    @tornado.gen.coroutine
    def test(self):
        print('a')
        a = 1
        self.cond = tornado.locks.Condition()
        yield self.cond.wait(timeout=datetime.timedelta(seconds=10))
        a = 2
        print('b')
        return a, 2

    @tornado.gen.coroutine
    def get(self):
        a, b = yield self.test()
        print(a, b)
        self.render("index.html")

这几个需要配合使用,使用得不好就可能会出现下面的错误:

AssertionError: yield from wasn't used with future
tornado.gen.BadYieldError: yielded unknown object
ValueError: not enough values to unpack (expected 2, got 0)
Relative Articles