Python Tornado:删除请求永不结束

2024-05-15 09:19:48 发布

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

我在Tornado中遇到删除请求的问题。请求到达服务器,处理程序中的一切都很好,但它从不向客户端返回响应。在

我试过返回一些东西,只有“返回”,甚至没有“返回”,结果总是一样的。在

我使用的是python3.4、Tornado 4.1和Firefox的RestClient。在

@web.asynchronous
@gen.coroutine
def delete(self, _id):
    try:
        model = Model()
        model.delete(_id)
        self.set_status(204)
    except Exception as e:
        logging.error(e)
        self.set_status(500)
    return

Tags: self服务器webid处理程序客户端modelstatus
1条回答
网友
1楼 · 发布于 2024-05-15 09:19:48

龙卷风文档(tornado.web.asynchronous):

If this decorator is given, the response is not finished when the method > returns. It is up to the request handler to call self.finish() to finish > the HTTP request.

你需要打电话tornado.web.RequestHandler。完成方法。这将起作用:

@web.asynchronous
@gen.coroutine
def delete(self, _id):
    try:
        model = Model()
        model.delete(_id)
        self.set_status(204)
    except Exception as e:
        logging.error(e)
        self.set_status(500)
    self.finish()
    return

但是,在这个例子中不需要异步方法。这也将以同样的方式工作:

^{pr2}$

另外,如果您使用@通用协同程序装饰师,你不需要使用@web.异步装饰工。只需使用@通用协同程序,这是正确的方式,更优雅。在

最后,我认为您应该阅读this article,以了解Tornado中的异步编程。在

相关问题 更多 >