一个简单的tornado+coroutine+yield的例子
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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")
这几个需要配合使用,使用得不好就可能会出现下面的错误:
1
2
3
AssertionError: yield from wasn't used with future
tornado.gen.BadYieldError: yielded unknown object
ValueError: not enough values to unpack (expected 2, got 0)
本文网址: https://pylist.com/topic/170.html 转摘请注明来源