龙卷风:来自mysql的更新数据未显示在龙卷风服务的前端。仅在重新启动服务器后显示

时间:2017-07-05 23:03:23

标签: python mysql angularjs tornado

我有一个龙卷风后端,为Angular前端提供服务。在更新数据库时,龙卷风api不会获取更新的数据。它仅在我重新启动服务器后出现。有人可以帮我这个吗?我希望获取的数据能够实时更新。

AngularJS ajax请求:

   $http({
            method: "GET",
            url: "http://localhost:9915/api/"
        }).then(function mySuccess(response) {
            console.log(response);
            $scope.data = response.data;
            sharedData.set(response.data);
            for (var i = $scope.data.length - 1; i >= 0; i--) {

                var coverageVal = $scope.data[i].Coverage;
                var coverageHealth = $scope.data[i].Health;

                if (coverageVal >= 95.00) {
                    $scope.data[i]["colorCoverage"] = "cyan";
                } else if (coverageVal >= 90.00 && coverageVal < 95.00) {
                    $scope.data[i]["colorCoverage"] = "warning";
                } else {
                    $scope.data[i]["colorCoverage"] = "danger";
                }

                if (coverageHealth >= 95.00) {
                    $scope.data[i]["colorHealth"] = "cyan";
                } else if (coverageHealth >= 90.00 && coverageHealth < 95.00) {
                    $scope.data[i]["colorHealth"] = "warning";
                } else {
                    $scope.data[i]["colorHealth"] = "danger";
                }

            }
        }, function myError(err) {
            console.log("error");
            console.log(err);
        });

Tornado api获取数据:

     class ApiRequestHandler(web.RequestHandler):
        def get(self):
           cur = conn.cursor()
           cur.execute("SELECT * from db.table")
           res = []
           for row in cur:
              res.append(row)
           self.write(json.dumps(res))

1 个答案:

答案 0 :(得分:0)

你没有说你使用的是什么mysql连接器,但是如果你使用的是mysqldb,请尝试这样的事情:

class ApiRequestHandler(web.RequestHandler):
    def get(self):
       cur = conn.cursor(MySQLdb.cursors.DictCursor) 
       cur.execute("SELECT * from db.table")
       rows = cur.fetchall()
       self.write(json.dumps(rows))

另外我建议你开始使用asyncio调用,比如使用python 3.5或者up,你可以使用aiomysql

class ApiRequestHandler(web.RequestHandler):
    async def get(self):
       cur = await conn.cursor() 
       await cur.execute("SELECT * from db.table")
       rows = await cur.fetchall()
       self.write(json.dumps(rows))