Tornado 6.0.3 来自 4.2:模块 'tornado.web' 没有属性 'asynchronous'
Tornado 6.0.3 from 4.2: module 'tornado.web' has no attribute 'asynchronous'
我已经从 tornado 4.2 转移到 tornado 6.0.3,我得到了错误
AttributeError: 模块 'tornado.web' 没有属性 'asynchronous'
根据 tornado v6 seems to have dropped tornado.web.asynchronous coroutine. any different way of fixing this in code?
中的讨论
我把@tornado.web.asynchronous改成了@tornado.gen.coroutine,这是固定的,接下来我得到
RuntimeError: 无法在 finish() 后写入 ()
根据RuntimeError: Cannot write() after finish(). May be caused by using async operations without the @asynchronous decorator
我在 write() 之后有 self.finish(),没有错误,但没有得到任何输出
这是我的代码
class MyHandler(RequestHandler):
_thread_pool = ThreadPoolExecutor(max_workers=10)
@tornado.gen.coroutine
def post(self):
try:
data = tornado.escape.json_decode(self.request.body)
self.predict('mod1')
except Exception:
self.respond('server_error', 500)
@concurrent.run_on_executor(executor='_thread_pool')
def _b_run(self, mod, X):
results = do some calculation
return results
@gen.coroutine
def predict(self, mod):
model = mod (load from database)
values = (load from database)
results = yield self._b_run(model, values)
self.respond(results)
def respond(self, code=200):
self.set_status(code)
self.write(json.JSONEncoder().encode({
"status": code
}))
self.finish()
虽然完成是在写入之后,但我没有得到输出
任何用 gen.coroutine
装饰的方法或函数都应该使用 yield
语句调用。否则协程不会运行。
因此,您需要在调用时 yield
predict
方法。
@gen.coroutine
def post(self):
...
yield self.predict('mod1')
...
我已经从 tornado 4.2 转移到 tornado 6.0.3,我得到了错误
AttributeError: 模块 'tornado.web' 没有属性 'asynchronous'
根据 tornado v6 seems to have dropped tornado.web.asynchronous coroutine. any different way of fixing this in code?
中的讨论我把@tornado.web.asynchronous改成了@tornado.gen.coroutine,这是固定的,接下来我得到
RuntimeError: 无法在 finish() 后写入 ()
根据RuntimeError: Cannot write() after finish(). May be caused by using async operations without the @asynchronous decorator
我在 write() 之后有 self.finish(),没有错误,但没有得到任何输出
这是我的代码
class MyHandler(RequestHandler):
_thread_pool = ThreadPoolExecutor(max_workers=10)
@tornado.gen.coroutine
def post(self):
try:
data = tornado.escape.json_decode(self.request.body)
self.predict('mod1')
except Exception:
self.respond('server_error', 500)
@concurrent.run_on_executor(executor='_thread_pool')
def _b_run(self, mod, X):
results = do some calculation
return results
@gen.coroutine
def predict(self, mod):
model = mod (load from database)
values = (load from database)
results = yield self._b_run(model, values)
self.respond(results)
def respond(self, code=200):
self.set_status(code)
self.write(json.JSONEncoder().encode({
"status": code
}))
self.finish()
虽然完成是在写入之后,但我没有得到输出
任何用 gen.coroutine
装饰的方法或函数都应该使用 yield
语句调用。否则协程不会运行。
因此,您需要在调用时 yield
predict
方法。
@gen.coroutine
def post(self):
...
yield self.predict('mod1')
...