python - Tornado redis错误: "finish() called twice"

标签 python redis tornado long-polling

class RedisHandler(BaseHandler):
@tornado.web.authenticated
@tornado.web.asynchronous
@tornado.gen.engine
def post(self):
    self.client = tornadoredis.Client()
    self.client.connect()
    yield tornado.gen.Task(self.client.subscribe,'notification')
    self.client.listen(self.on_message)

def on_message(self,msg):
    if msg.kind == 'message':
        self.finish(dict(complete=True,message=msg.body))
    return

上面的代码引发了:

RuntimeError: finish() called twice

可能是没有@asynchronous装饰器使用异步操作导致的。

最佳答案

可能是因为你在调用self.finish()的时候没有取消订阅/断开Redis的连接,所以当另一条消息到达时,调用了on_message()再次:

def on_message(self,msg):
    if msg.kind == 'message':

        self.finish(dict(complete=True,message=msg.body))

        self.client.unsubscribe('notification')
        self.client.disconnect()

关于python - Tornado redis错误: "finish() called twice",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19712183/

相关文章:

python - 用于网络抓取的旋转代理

windows - MongoDB 作为 Windows 上的纯内存数据库?

python - 在 Tornado 中处理长请求永远不会完成

python - Rabbitmq 使用 Tornado 不阻塞消息

python - 为什么要同时使用 Tornado 和 Flask?

python - 本地 HTML 文件无法正确加载到 Dash 应用程序中

python - NotADirectoryError : [WinError 267] The directory name is invalid error while invoking Firefox through Selenium Python

python - Keras 中的 "Could not interpret activation function identifier: 256"错误

Redis在获取键值时响应各种$值

amazon-ec2 - Redis - 扩展不同的 ec2 区域