Python 使用灯芯绒couchdb异步库时tornado.gen中的Badyielder错误
我试图使用python与CouchDB异步对话 下面的代码来自,经过一些修改Python 使用灯芯绒couchdb异步库时tornado.gen中的Badyielder错误,python,couchdb,tornado,Python,Couchdb,Tornado,我试图使用python与CouchDB异步对话 下面的代码来自,经过一些修改 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:
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()
我遇到的问题是,我收到了一个错误,尽管我发现沙发文件非常好。我怀疑这与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}>
回溯(最近一次呼叫最后一次):
文件“c:\env\pyfines\u tornado\lib\site packages\tornado\web.py”,第1074行,在包装器中
返回方法(self、*args、**kwargs)
文件“c:\env\pyfines\u tornado\lib\site packages\corduroy\\uuuuuuu init\uuuuuuuuuuuuuuux”第43行_
返回生成引擎(_func_u)(*args,**kwargs)
文件“c:\env\pyfines\u tornado\lib\site packages\tornado\gen.py”,第107行,在包装器中
runner.run()
文件“c:\env\pyfines\u tornado\lib\site packages\tornado\gen.py”,第319行,正在运行
屈服=自我生成抛出(*exc_信息)
get中第10行的文件“test.py”
doc=yield people\u db.get(用户id)
Badyielder错误:生成了未知对象
阅读,在我看来,灯芯绒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()
我还没有测试过这个,让我知道它是怎么回事 您是否尝试过直接使用
gen
模块?还没有。我还在学习龙卷风,以前从未使用过。我以后可能会玩一玩,谢谢!直接使用gen模块会产生很好的效果,但是我仍然想尝试灯芯绒。今晚再次调查…我不确定@RELAXE decorator的质量。试着深入研究它的代码,了解它正在使用的tornado的哪些功能。@relax decorator看起来就像是在用异步和tornado.gen.engine decorators来代替。但是,我仍然必须使用“doc=yield tornado.gen.Task(people\u db.get,user\u id)”来做任何事情:(谢谢你的回答。我似乎记得做过类似的事情,但我现在已经停止使用tornado,所以我必须将它留给其他人来验证这是正确的。