python flask 实时刷新数据,Python Flask日期更新实时

I am building a web app with Python Flask with JavaScript. I am a beginner of Javascript.

The process I do now:

In Flask Python code,

1. I get data by scrapping the web (numeric data that updates every minute).

2. use the data and calculate something and get the final numbers.

3. make a list that contains the final numbers

4. feed the list to a page by adding the list to the page's definition of Flask

5. Now in HTML get the list by capturing it with {{ data|safe }} tag

6. use it with Javascript to make a chart.

The problem is:

In the step 1, the data I get is being updated every minute. For example, on that web page now there are 15 data points. I parse the last 10 data points from that web page and then I put them in a list in my Python and then do the following steps and make a chart on my webpage. One minute later, in the data source web page, there will be 16 data point available, and I need to get the last 10 data points. In that case, I need to run the python code again to get the latest 10 data points to use them to make a chart on my web page.

So, I need to always run the whole python code which is the whole Flask app init.py file and re-render my webpage to see the updated chart. If I do not re-run the init.py file in my server, then even after 10 minutes or 2 hours, I will only see the data that I parsed for the first time forever.

How should I run the Flask and always get the updated data without always re-run the flask init.py every time.

I thought about using time.sleep(60) so that the flask app python file is run every 1 minutes. But this is really taking alot of time when my code gets much more thinks to calculate. And do not really work.

How should I solve this problem??

Should I use time.sleep ? or is threre better way?

解决方案

This is a classic "push" update problem. Your web server is updated with information that you would like to frequently update (or "push") to your web client front-end.

As PJ Santoro suggests, you can repeatedly poll your server (every 10 seconds say) along the lines of "Is there new data? No. Okay. Is there new data? No. Okay. Is there new data? Yes, here it is! Great! Is there new data? ...." It is rather inefficient, but for low data volumes you might never see that problem.

A more efficient update system would have the server send an update signal only when new data is ready. There are two major ways to do that:

Long polling, also known as reverse AJAX and here is an example using Flask.

WebSockets. WebSockets are a much more recent approach to interactive and "push" updates between servers and clients. They're very well-used; projects like the Jupyter Notebook depend on them extensively. They're extremely efficient and well-fit for this kind of incremental update of client state, and the code is often much less Byzantine and confusing than retrofits like reverse AJAX. But...there are complexities with WebSockets too. Managing concurrency in the Flask server side is still a significant issue, for example. If you'd like to consider WebSockets as your update mechanism, here is an example of how to use them with Flask.

Either approach you use, if your data communicated over time grows, you will also need to structure the data transmitted on each update so that it's an incremental update. No mater how good the plumbing, if you transmit thousands of data points every update, it's not going to be fast. But, long polling and WebSockets plumbing should at least get you a great distance toward a legitimate real-time update capability.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值