问题描述
我在 Tornado 服务器中发出发布请求时遇到上述错误.
I am getting the above error while making a post request in the tornado server .
我有一个类
class RestRequestHandler(RequestHandler):
def async_get(self, *args, **kwargs):
pass
def async_post(self, *args, **kwargs):
pass
上面的类我导入到另一个py文件中
The above class I am importing into another py file
class get_question_details(RestRequestHandler):
def async_post(self, activity_id, attempt_id, quiz_id, question_id,
questionusage_id, slot, user_name, courseshortname):
client = get_moodle_client()
当我提出这个请求时,我收到了像
When I am making this request I am getting the warning like
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/tornado/ioloop.py", line 421, in _run_callback
callback()
File "/usr/local/lib/python2.7/dist-packages/gameonapi/server/rest.py", line 142, in async_post_callback
self.return_ok(result)
File "/usr/local/lib/python2.7/dist-packages/gameonapi/server/rest.py", line 214, in return_ok
self.write (result)
File "/usr/local/lib/python2.7/dist-packages/tornado/web.py", line 489, in write
raise RuntimeError("Cannot write() after finish(). May be caused "
RuntimeError: Cannot write() after finish(). May be caused by using async operations without the @asynchronous decorator.
从错误中我明白了这个问题的原因是因为我没有使用龙卷风 @asynchronous
但在使用之后我仍然收到同样的警告.
From the error I understood that problem causing because I am not using the tornado @asynchronous
but after using that also still I am getting the same warning .
请告诉我我在这里做错了什么.
Please tell me what might I am doing wrong here .
推荐答案
如果你不使用 @asynchronous
装饰器 self.finish()
函数将被调用功能完成前自动
If you do not use the @asynchronous
decorator the self.finish()
function would be called automatically before the function is complete
你应该拥有的是
class RestRequestHandler(RequestHandler):
@tornado.web.asynchronous
def async_get(self, *args, **kwargs):
self.write('ok')
self.finish()
@tornado.web.asynchronous
def async_post(self, *args, **kwargs):
self.write('ok')
self.finish()
通过这种方式,系统让您有机会写出您想写的任何内容.完成编写后,您需要调用 self.finish
方法来告诉系统您已完成.
This way the system gives you the chances to write out whatever you wish to write. When finished writing you will need to call the self.finish
method to tell the system you are done.
这篇关于运行时错误:无法在完成()后写入().可能是由于使用没有 @asynchronous 装饰器的异步操作引起的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!