Badyielderro输入龙卷风.gen使用Corduroy couchdb异步库时

2024-09-30 06:15:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用Corduroy来使用pythonTornado 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()

我遇到的问题是,尽管couch文档非常好,但我还是收到了一个badyieldror。我怀疑这和龙卷风.gen模块设置不正确(或其他原因?)。使用灯芯绒而不使用@relax decorator,并带有回调函数。在

^{pr2}$

Tags: importselfwebiddbgetdocpeople
1条回答
网友
1楼 · 发布于 2024-09-30 06:15:35

阅读the code for relax和{a2},在我看来,灯芯绒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 ^{} is in the Tornado documentation。在

相关问题 更多 >

    热门问题