Pymysql通过tornado websocket从表中返回相同的结果而不是最后一行

时间:2018-05-30 06:58:53

标签: tornado pymysql

我需要一些建议。我有一个基于龙卷风框架的项目应用程序,我使用WebSocketHandler。客户端每10秒发送一次请求。 WS处理程序实现的一部分如下:

class WebSocketHandler(tornado.websocket.WebSocketHandler):

    def __init__(self, *args, **kwargs):
        self.db_passwrod = kwargs.pop('password')
        self.db_user = kwargs.pop('user')
        self.db_name = kwargs.pop('db_name')
        self.db_connection = db.DbConnection(password=self.db_passwrod,
                                             user=self.db_user,
                                             db_name=self.db_name,
                                             cursor_type=True
                                             )
        super(WebSocketHandler, self).__init__(*args, **kwargs)

    def EnergyDB(self):
        return self.db_connection.fetch_row(table="Energy",
                                            value="ORDER BY Timestamp DESC LIMIT 1",
                                            flag=None)


    def convert(self, object):
        if isinstance(object, datetime.datetime):
            return object.__str__()

    def open(self, *args):
        print("Websocket up")
        self.write_message("coonection established")

    def on_message(self):
        self.write_message(json.dumps(self.EnergyDB))

    def on_close(self):
        print("websocket goes down")

数据库类是这样的:

class DbConnection():

    def __init__(self,
                 host='localhost',
                 port=3306,
                 user='user',
                 password='password',
                 db_name=None,
                 cursor_type=None):
                 self.connection = pymysql.connect(host=host,
                                                   user=user,
                                                   passwd=password,
                                                   port=port,
                                                   database=db_name,
                                                   charset='utf8')
        if cursor_type is None:
            self.cursor = self.connection.cursor(pymysql.cursors.SSCursor)
        else:
            self.cursor = self.connection.cursor(pymysql.cursors.SSDictCursor)

    def fetch_row(self, data='*', table=None, vartype=None, value=None, flag=None):
        if flag is not None:
            if value is None:
                self.cursor.execute("SELECT %s FROM %s" %(data, table))
            else:
                self.cursor.execute("SELECT %s FROM %s WHERE %s" %(data, table, value))
        else:
            self.cursor.execute("SELECT %s FROM %s WHERE %s = %s" %(data, table, vartype, value))

         return self.cursor.fetchall()

我结束了同样的线路,也许是在客户端的第一次呼叫时收集,而不是最后添加。但是每一秒都会从机器传感器连接中添加新线。所以,如果我做对了。 pmyysql游标的每个创建对象出于安全原因从创建游标的时刻开始使用表格的图像/防止损坏数据库并在创建curosr后忽略更改?因此,我需要创建新游标,收集值并删除curosr每个请求? Becasue这是第一种方法,它工作正常,但有大量的CPU过载。或者我错过了什么?

1 个答案:

答案 0 :(得分:0)

  1. 在您的示例代码中,您实际上并未调用EnergyDB函数。我假设丢失的括号只是一个错字。

  2. 这里的主要问题是您正在重用游标和事务,因此您可以看到数据库的一致快照。每次使用它之后,您需要运行self.connection.commit()self.connection.abort()来重置连接并能够查看新数据(只要您的查询是只读的,提交和中止是等效的)。我不确定重复使用这样的游标是否安全,或者你是否应该每次都创建一个新游戏(在我自己的代码中我总是创建一个新游标)。

  3. 不要使用%运算符来构造SQL查询。这很容易受到SQL注入攻击。而是使用数据库驱动程序的参数替换功能,如pymysql's example

  4. 中所示