Python Flask日期实时更新 [英] Python Flask date update real-time

查看:475
本文介绍了Python Flask日期实时更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有JavaScript的Python Flask构建一个Web应用程序.我是Java语言的初学者.

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

我现在正在执行的过程:

The process I do now:

在Flask Python代码中,
1.我通过抓取网络来获取数据(每分钟更新一次的数值数据). 2.使用数据并计算得出最终数字. 3.制作一个包含最终号码的列表 4.通过将列表添加到页面的Flask定义中,将列表提供给页面 5.现在,在HTML中,使用{{data | safe}}标签捕获列表

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

问题是: 在第1步中,我获取的数据每分钟都会更新一次.例如,现在该网页上有15个数据点.我解析了该网页的最后10个数据点,然后将它们放在Python的列表中,然后执行以下步骤并在网页上绘制图表.一分钟后,在数据源网页中,将有16个数据点可用,我需要获取最后10个数据点.在这种情况下,我需要再次运行python代码以获取最新的10个数据点,以便使用它们在我的网页上绘制图表.

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.

因此,我需要始终运行整个python代码(即整个Flask应用程序的init.py文件),然后重新渲染我的网页以查看更新后的图表.如果我不重新运行服务器中的init.py文件,那么即使经过10分钟或2个小时,我也只会看到我永远第一次解析的数据.

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.

我应该如何运行Flask并始终获取更新的数据,而不必每次都始终重新运行flask init.py.

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

我考虑过使用time.sleep(60)来使flask应用程序python文件每1分钟运行一次.但是,当我的代码需要更多的计算能力时,这确实要花费很多时间.而且真的不行.

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??

我应该使用time.sleep吗?还是还有更好的方法?

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

推荐答案

这是经典的推送"更新问题.您的Web服务器已更新,其中包含您要经常更新(或推送")到Web客户端前端的信息.

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.

按照 PJ Santoro的建议,您可以按照是有新数据吗?没有.有没有新数据?没有.有新数据吗?是的,这是这里!太棒了!有新数据吗?您可能永远不会看到这个问题.

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:

  1. 长期轮询,也称为反向AJAX 彗星 .您的网页在服务器URL(例如/update_data)上打开AJAX请求,超时时间非常长(例如30分钟或一个小时).服务器拥有数据后,它将通过等待的AJAX连接发送数据.这样做有一些复杂性,包括管理服务器端的并发性(从历史上看不是Python的强项),以及即使最大超时(我使用此技术的时间大约是1个小时)也可能在没有数据的情况下过期,并且需要一些错误处理才能做到这一点.处理.如果您想尝试长时间轮询/反向AJAX,请此处是使用Flask的示例.

  1. Long polling, also known as reverse AJAX and Comet. Your web page opens an AJAX request on a server URL such as /update_data with a really long timeout (say 30 minutes or an hour). When the server has data, it sends it through the waiting AJAX connection. There are complications with this, including managing concurrency on the server side (not historically Python's strong suit) and the fact that even the maximum timeout (~1 hour last I used this technique) might expire without data, and some error handling is needed to manage that. If you wan to try long polling / reverse AJAX, here is an example using Flask.

WebSockets . WebSocket是一种更新的方法,用于在服务器和客户端之间进行交互式和推送"更新.它们被很好地使用. Jupyter Notebook 之类的项目在很大程度上取决于它们.它们非常有效并且非常适合这种客户端状态的增量更新,并且与反向AJAX这样的改型相比,它们的拜占庭式代码和令人困惑的代码通常要少得多.但是... WebSockets也很复杂.例如,在Flask服务器端管理并发仍然是一个重要问题.如果您想将WebSockets视为更新机制,请此处是一个示例与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.

无论使用哪种方法,如果随着时间的推移所传递的数据不断增长,则还需要对每次更新中传输的数据进行结构化,以使其成为增量更新.不管如何,管道的性能有多好,如果每次更新都传输数千个数据点,那将不会很快.但是,长时间的轮询和WebSockets检测至少应该使您距离合法的实时更新功能还有一段很长的距离.

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.

这篇关于Python Flask日期实时更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆