繁体   English   中英

在龙卷风中,如何使这些功能异步?

[英]In tornado ,how to make these functions async?

这是我的代码:

#/test
class Test(tornado.web.RequestHandler):
    @tornado.web.asynchronous
    @tornado.gen.coroutine
    def get(self):
        res = yield self.inner()
        self.write(res)

    @tornado.gen.coroutine
    def inner(self):
        import time
        time.sleep(15)
        raise tornado.gen.Return('hello')

#/test_1
class Test1(tornado.web.RequestHandler):
    @tornado.web.asynchronous
    @tornado.gen.coroutine
    def get(self):
        res = yield self.inner()
        self.write(res)

    @tornado.gen.coroutine
    def inner(self):
        raise tornado.gen.Return('hello test1')

当我获取/ test然后获取/ test_1时,/ test_1直到/ test响应后才响应,如何解决呢?

不要使用time.sleep() time.sleep()将阻止CPU循环。 相反,使用

yield tornado.gen.Task(tornado.ioloop.IOLoop.instance().add_timeout,
                               time.time() + sleep_seconds)

您遇到了两个常见问题:

http://www.tornadoweb.org/en/stable/faq.html

首先,请不要在Tornado应用程序中使用time.sleep(),而应使用gen.sleep()。 其次,请注意,大多数浏览器不会同时从同一域中获取两个页面:请使用“ curl”或“ wget”来测试您的应用程序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM