我正在尝试使用Corduroy使用python Tornado Web Server以异步方式与CouchDB通信。
以下代码来自Corduroy guide,并进行了一些更改。
import tornado.web
from corduroy import Database, NotFound, relax
people_db = Database('people')
class RelaxedHello(tornado.web.RequestHandler):
@relax
def get(self, user_id):
try:
doc = yield people_db.get(user_id)
self.write('hello %s'%(doc['name']))
except NotFound:
self.write('hello whoever you are')
self.finish()
application = tornado.web.Application([
(r'/hi/([^/]+)', RelaxedHello),
]).listen(1920)
tornado.ioloop.IOLoop.instance().start()
我遇到的问题是,尽管找到了沙发文档,但我收到了BadYieldError。我怀疑这与tornado.gen模块没有正确设置(或其他什么?)有关。使用没有@relax装饰器的灯芯绒,并使用回调工作正常。
Traceback (most recent call last):
File "c:\env\pymeals_tornado\lib\site-packages\tornado\web.py", line 1074, in wrapper
return method(self, *args, **kwargs)
File "c:\env\pymeals_tornado\lib\site-packages\corduroy\__init__.py", line 43, in _r_e_l_a_x_
return gen.engine(_func_)(*args, **kwargs)
File "c:\env\pymeals_tornado\lib\site-packages\tornado\gen.py", line 107, in wrapper
runner.run()
File "c:\env\pymeals_tornado\lib\site-packages\tornado\gen.py", line 319, in run
yielded = self.gen.throw(*exc_info)
File "test.py", line 10, in get
doc = yield people_db.get(user_id)
BadYieldError: yielded unknown object <Document 65d936ee54417e46479a908f7a0038ef[2] {name:Colin}>
答案 0 :(得分:0)
阅读the code for relax和for get(),在我看来,灯芯绒3岁的代码并非专为最现代的龙卷风成语而设计。具体来说,get
采用回调,而不是像现代Tornado API那样返回Future
。
这意味着您必须使用gen.Task
之类的旧款式:
from tornado import gen
class RelaxedHello(tornado.web.RequestHandler):
@relax
def get(self, user_id):
try:
doc = yield gen.Task(people_db.get, user_id)
self.write('hello %s'%(doc['name']))
except NotFound:
self.write('hello whoever you are')
self.finish()
我还没有对此进行测试,让我知道它是怎么回事。 More info on gen.Task
is in the Tornado documentation