Python Flask日期实时更新(Python Flask date update real

编程入门 行业动态 更新时间:2024-10-25 16:26:59
Python Flask日期实时更新(Python Flask date update real-time)

我正在使用带有JavaScript的Python Flask构建一个Web应用程序。 我是Javascript的初学者。

我现在做的过程:

在Flask Python代码中, 1.我通过废弃网络获取数据(每分钟更新的数字数据)。 2.使用数据并计算一些东西并获得最终数字。 3.创建一个包含最终数字的列表4.通过将列表添加到页面对Flask 5的定义来将列表提供给页面。现在在HTML中通过使用{{data | safe}}标记捕获列表来获取列表

6.使用它与Javascript制作图表。

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

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

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

我考虑过使用time.sleep(60)以便每隔1分钟运行一次烧瓶app python文件。 但是,当我的代码更多地考虑计算时,这确实需要很多时间。 并没有真正的工作。

我该如何解决这个问题?

我应该使用time.sleep吗? 或者是更好的方式?

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?

最满意答案

这是一个经典的“推送”更新问题。 您的Web服务器将更新为您希望经常更新(或“推送”)到Web客户端前端的信息。

正如PJ Santoro建议的那样 ,您可以反复轮询您的服务器(每10秒钟一次)沿着“是否有新数据?没有。好的。是否有新数据?没有。好的。是否有新数据?是的,这里是好极了!有新数据吗?......“它效率很低,但对于低数据量,你可能永远不会看到这个问题。

更高效的更新系统将使服务器仅在新数据准备好时发送更新信号。 有两种主要方法可以做到这一点:

长轮询 ,又称反向AJAXComet 。 您的网页在服务器URL上打开一个AJAX请求,例如/update_data ,其超时时间非常长(例如30分钟或一小时)。 当服务器有数据时,它通过等待的AJAX连接发送它。 这有很多复杂的问题,包括管理服务器端的并发(不是历史上Python的强项)以及甚至最大超时(我最后使用这种技术约1小时)可能在没有数据的情况下到期,并且需要一些错误处理管理那个。 如果你想尝试长轮询/反向AJAX, 这里有一个使用Flask的例子 。

WebSockets 。 WebSockets是服务器和客户端之间交互式和“推送”更新的最新方法。 它们使用得很好; 像Jupyter笔记本这样的项目广泛依赖于它们。 它们非常高效且非常适合客户端状态的这种增量更新,并且代码通常比反向AJAX等改造更容易被拜占庭和混乱。 但是...... WebSockets也存在复杂性。 例如,在Flask服务器端管理并发性仍然是一个重要问题。 如果您想将WebSockets视为更新机制,下面是一个如何将它们与Flask一起使用的示例 。

您使用的任何一种方法,如果您的数据随着时间的推移而增长,您还需要构建每次更新时传输的数据,以便进行增量更新。 无论管道有多好,如果每次更新都传输数千个数据点,那就不会很快。 但是,长轮询和WebSockets管道应该至少让您与合法的实时更新功能保持距离。

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

更多推荐

本文发布于:2023-08-07 08:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1462276.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实时   日期   Flask   Python   update

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!