Currently I have a Flask server that runs a small web frontend as well as a command line interface to that same server. The basic idea looks like this:
目前我有一个Flask服务器,它运行一个小的Web前端以及一个到同一服务器的命令行界面。基本思路如下:
<Top section: allows file upload>
* list of files from database
<Second section: allows file manipulation/ upload>
* list of files from database
<Third section: Not files, but still allows for database changes>
* list of things made from database
Now this works well from the front end, but currently if the CLI or another client makes a change to the database, it doesn't update other clients. I have it somewhat working with JS polling and rewriting the list of files every 10s, but that seems both inefficient and also would look very messy if I had to do it for every section. I saw websockets mentioned in various forums, but I've never used them and am unsure if it would be a pain to add. I'm not trying to rewrite the whole thing for a single feature.
现在这在前端运行良好,但是目前如果CLI或其他客户端对数据库进行了更改,它不会更新其他客户端。我有点使用JS轮询和每10秒重写一次文件列表,但这看起来效率低下,如果我必须为每个部分做这件事,看起来也会非常混乱。我看到各种论坛中提到的websockets,但我从未使用它们,我不确定添加是否会很痛苦。我不是要为一个功能重写整个事情。
Final takeaway: How to update all clients better than polling/ how to do polling efficiently?
最后一点:如何更好地更新所有客户端而不是轮询/如何有效地进行轮询?
1 个解决方案
#1
2
Yes you are correct. You need sockets. There are bunch of articles over the internet but I would like to give a summary and try to explain why sockets will be the best fit to your requirements.
是的,你是对的。你需要插座。互联网上有大量文章,但我想提供一个摘要,并尝试解释为什么套接字最符合您的要求。
Sockets are way of achieving two way communication between client and server without the need of polling.
套接字是在客户端和服务器之间实现双向通信而无需轮询的方式。
There is a package called Flask-SocketIO
有一个名为Flask-SocketIO的软件包
Flask-SocketIO gives Flask applications access to low latency bi-directional communications between the clients and the server.
Flask-SocketIO使Flask应用程序能够访问客户端和服务器之间的低延迟双向通信。
Then for the scenario where you would like to send changes to all the connected client when one client does some work to your database or something similar, you will need to use broadcasting. When a message is sent with the broadcast option enabled, all clients connected to the namespace receive it, including the sender. Here you can find details of the broadcasting using Flask-SocketIO.
然后,对于您希望在一个客户端对您的数据库或某些类似的东西进行某些工作时向所有连接的客户端发送更改的场景,您将需要使用广播。在启用广播选项的情况下发送消息时,连接到命名空间的所有客户端都会接收它,包括发件人。在这里,您可以使用Flask-SocketIO找到广播的详细信息。
#1
2
Yes you are correct. You need sockets. There are bunch of articles over the internet but I would like to give a summary and try to explain why sockets will be the best fit to your requirements.
是的,你是对的。你需要插座。互联网上有大量文章,但我想提供一个摘要,并尝试解释为什么套接字最符合您的要求。
Sockets are way of achieving two way communication between client and server without the need of polling.
套接字是在客户端和服务器之间实现双向通信而无需轮询的方式。
There is a package called Flask-SocketIO
有一个名为Flask-SocketIO的软件包
Flask-SocketIO gives Flask applications access to low latency bi-directional communications between the clients and the server.
Flask-SocketIO使Flask应用程序能够访问客户端和服务器之间的低延迟双向通信。
Then for the scenario where you would like to send changes to all the connected client when one client does some work to your database or something similar, you will need to use broadcasting. When a message is sent with the broadcast option enabled, all clients connected to the namespace receive it, including the sender. Here you can find details of the broadcasting using Flask-SocketIO.
然后,对于您希望在一个客户端对您的数据库或某些类似的东西进行某些工作时向所有连接的客户端发送更改的场景,您将需要使用广播。在启用广播选项的情况下发送消息时,连接到命名空间的所有客户端都会接收它,包括发件人。在这里,您可以使用Flask-SocketIO找到广播的详细信息。