简体   繁体   中英

Fetching data after a certain time interval(10 sec) from a continuously increasing database like mysql using flask

我想使用python和flask创建一个api,它以固定的时间间隔(10秒)从不断增长的数据库中连续提取和存储数据,而我不想获取已经获取的旧数据。

Say you currently have an API endpoint that returns all the database stored data:

@app.route('/data', methods=['post'])
def data():
    all_the_data = Data.query.order_by(Data.created.desc()).all()
    return jsonify(results=all_the_data)

So your ajax call currently doing something like:

$.ajax({
    type: "POST",
    url: "/data",
    dataType: "json",
    success: function(data) { 
        console.log(data);
        update_graph(data);
    }
});

You just need a way for the system to filter what's going out, back to the client-- so we instead of querying all the data, we can filter based on a reference:

@app.route('/data', methods=['post'])
def data():
    client_data = request.json
    reference = client_data.get('reference')

    if reference:
        # we have a reference, so lets filter the query:
        query_data = Data.query.filter(Data.id>reference).order_by(Data.created.desc()).all()
    else:
        # no reference, so send the lot!
        query_data = Data.query.order_by(Data.created.desc()).all()

    return jsonify(results=query_data)

Then your ajax request needs to get the last reference from the last query it did-- and supply that to the API endpoint:

$.ajax({
    type: "POST",
    url: "/data",
    data: JSON.stringify({ reference: 999 }),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(data) {
       console.log(data)
       update_graph(data["results"]);
    }
});

So you just need to work out how to get that reference value from the last set of values you recieved (the API could send that back as another key, or you could poll your current set within javascript, etc).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM