简体   繁体   English

在一定时间间隔(10秒)后使用flask从不断增加的数据库(如mysql)中获取数据

[英]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: 假设您当前有一个API端点,该端点返回所有数据库存储的数据:

@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调用当前正在执行以下操作:

$.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请求需要从它执行的上一个查询中获取最后一个引用-并将其提供给API端点:

$.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). 因此,您只需要弄清楚如何从您收到的最后一组值中获取该reference值(API可以将其作为另一个键发送回去,或者您可以在javascript中轮询当前的组,等等)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过从 excel 文件中获取数据并在 5 秒后使用 python 在图形中绘制数据来显示实时图形 - How do I display real-time graphs from fetching data from excel file and plotting the data after 5 sec in graph using python python脚本在特定时间间隔后连续轮询文件夹并将新文件复制到另一个位置 - python script to poll a folder continuously after a certain time interval and copy the new files to another location Python可靠的项目设置:在一定时间间隔内将数据从串行端口写入数据库 - Python reliable project setup: write data from a serial port to a database in a certain time interval 无法使用 flask 将 MySQL 数据库中的数据检索到 html - Not able to retrieve data from a MySQL database into html using flask 使用 Flask (Python) 从 MySQL 数据库中获取“一些”数据 - Fetch "Some" data from MySQL Database using Flask (Python) 工作 10-15 秒后烧瓶关闭 - Flask shutdown after working 10-15 sec 在一定时间间隔后自动从Google App Engine注销 - Logout automatically from google app engine after certain time interval 使用python从数据库中获取数据 - Fetching data from database using python 即使使用 connection.commit() 也无法将数据提交到数据库(使用 Python flask MySQL) - Not able to Commit data to database (using Python flask MySQL) even after using connection.commit() Django:如何在从mysql DB提取数据时使用LIKE - Django : How to use LIKE while fetching data from mysql DB
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM