龙卷风协程 [英] Tornado coroutine

查看:31
本文介绍了龙卷风协程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习 Tornado 协程,但使用以下代码时出错.

I am trying to learn tornado coroutines, but I have error using below code.

Traceback (most recent call last):
  File "D:\projekty\tornado\env\lib\site-packages\tornado\web.py", line 1334, in _execute
    result = yield result
  File "D:\projekty\tornado\env\lib\site-packages\tornado\gen.py", line 628, in run
    value = future.result()
  File "D:\projekty\tornado\env\lib\site-packages\tornado\concurrent.py", line 109, in result
    raise_exc_info(self._exc_info)
  File "D:\projekty\tornado\env\lib\site-packages\tornado\gen.py", line 631, in run
    yielded = self.gen.throw(*sys.exc_info())
  File "index.py", line 20, in get
    x = yield 'test'
  File "D:\projekty\tornado\env\lib\site-packages\tornado\gen.py", line 628, in run
    value = future.result()
  File "D:\projekty\tornado\env\lib\site-packages\tornado\concurrent.py", line 111, in result
    raise self._exception
BadYieldError: yielded unknown object 'test'

代码:

from tornado.ioloop import IOLoop
from tornado.web import RequestHandler, Application, url
from tornado import gen

class HelloHandler(RequestHandler):
    @gen.coroutine
    def get(self):
        x = yield 'test'
        self.render('hello.html')


def make_app():
    return Application(
        [url(r"/", HelloHandler)], 
        debug = True
    )

def main():
    app = make_app()
    app.listen(8888)
    IOLoop.instance().start()

main()

推荐答案

正如 Lutz Horn 所指出的,tornado.coroutine 装饰器要求您只生成 Future 对象或某些包含 Future 对象的容器.所以试图产生一个 str 会引发一个错误.我认为您缺少的部分是协程中您想要调用 yield something()something 的任何地方也必须是协程,或者返回未来.例如,您可以像这样修复您的示例:

As Lutz Horn pointed out, the tornado.coroutine decorator requires that you yield only Future objects or certain containers containing Future objects. So trying to yield a str will raise an error. I think the piece you're missing is that any place inside of a coroutine where you want to call yield something(), something must either also be a coroutine, or return a Future. For example, you could fix your example like this:

from tornado.gen import Return

class HelloHandler(RequestHandler):
    @gen.coroutine
    def get(self):
        x = yield self.do_test()
        self.render('hello.html')

    @gen.coroutine
    def do_test(self):
        raise Return('test')
        # return 'test' # Python 3.3+

甚至这个(虽然通常你不应该这样做):

Or even this (though generally you shouldn't do it this way):

class HelloHandler(RequestHandler):
    @gen.coroutine
    def get(self):
        x = yield self.do_test()
        self.render('hello.html')

    def do_test(self):
        fut = Future()
        fut.set_result("test")
        return fut

当然,这些都是人为的例子;因为我们实际上并没有在 do_test 中做任何异步操作,所以没有理由让它成为协程.通常你会在那里做某种异步 I/O.例如:

Of course, these are contrived examples; since we're not actually doing anything asynchronous in do_test, there's no reason to make it a coroutine. Normally you'd be doing some kind of asynchronous I/O in there. For example:

class HelloHandler(RequestHandler):
    @gen.coroutine
    def get(self):
        x = yield self.do_test()
        self.render('hello.html')

    @gen.coroutine
    def do_test(self):
        http_client = AsyncHTTPClient()
        out = yield http_client.fetch("someurl.com") # fetch is a coroutine
        raise Return(out.body)
        # return out.body # Python 3.3+

这篇关于龙卷风协程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆