用Django和bokeh绘制实时数据的正确方法 [英] Right way to plot live data with django and bokeh

查看:266
本文介绍了用Django和bokeh绘制实时数据的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Django应用中嵌入了一个散景图.我在django视图中创建该图,然后将其推到bokeh服务器以在我的网页中显示该图.

i have a bokeh plot embedded in a django app. I create the plot in the django view and push the plot to the bokeh server to show the plot in my webpage.

#view.py
def view_plot(request):
    f=figure()
    f.plot(#some data#)
    session = push_session(curdoc())
    context = {'script': autoload_server(f, session_id=session.id)}
    return render_to_response('plot.html', context=context)

一切正常.现在,我想进行实时绘图,每次创建新的DB-Entry时,都应更新绘图.我不确定什么是最好的方法.

It all works quite good. Now I want to do a live plot, every time when a new DB-Entry is created the plot should be updated. I'm not sure what is the best way.

在网页上使用计时器查询现在的数据是一种好习惯吗?

Is it a good practice to use a timer on the webpage to ask for now data?

或者是否有办法将更新从服务器推送到服务器,以便每个当前连接的客户端都能获得绘图更新?

Or is there a way to push the update form the server so that every currently connected client gets the plot update?

我非常感谢每一个提示.

I would be very thankful for every hint.

非常感谢.

推荐答案

基本上,这里的问题是浏览器使用请求-响应模式:他们发送请求,然后立即获得答案.您有两种选择,定期轮询服务器或某种通知系统.

Basically your problem here is that browsers use a request-response pattern: they send a request and then get back an answer immediately. You have two options, polling the server periodically or some kind of notification system.

通知可以是长期轮询,也就是说,客户端发出请求,而服务器只有在有数据,通过websocket或通过HTML5服务器端事件进行响应后才响应.

Notifications could be long-polling, i.e. client makes a request and server doesn't respond until there's data, or through websockets or through HTML5 server-side events.

现在,问题在于这些通知系统与传统的Django部署集成得不太好,因为它们导致打开的套接字和相应的挂起线程.因此,如果您的网络服务器有10个Django线程,则一个带有10个选项卡的浏览器可能会将所有线程捆绑在一起.

Now, the thing is that these notification systems don't integrate too well with a traditional Django deployment, since they result in an open socket and a corresponding hanging thread. So if your webserver has 10 Django threads, one browser with 10 tabs could tie up all of them.

正在对此进行改变,但是与此同时,除非您有严格的实时要求或有很多客户端,否则只需设置一个计时器并每隔x秒轮询一次,其中x取决于可接受的延迟时间.根据您数据的存储方式,我可能会采用一种简单的机制,以使服务器不会每次都发送整个数据集,而不会发送所有新数据,也不会发送任何随身携带的返回代码.

Work is underway to change that, but in the mean time, unless you have a hard real-time requirement or lots of clients, just set a timer and poll every x seconds, where x depends on what latency is acceptable. Depending on how your data is stored, I would probably put in a simple mechanism so that the server doesn't send the whole dataset each time, but only either what's new or a carry-on-nothing-changed return code.

例如,在第一个请求上,服务器可以在响应中输入时间戳或序列号,然后客户端请求自该时间戳/序列号以来的任何更改.

For instance, on the first request, the server may put in a timestamp or serial number in the response, and then the client asks for any changes since that timestamp/serial number.

通知系统为您提供了更好的延迟和更低的开销,但是它可能也将更难以部署,并且如果这只是一个内部使用的应用程序,则可能会被淘汰.即使使用通知系统,您也需要进行一些仔细的协议设计,以确保不会错过任何内容.

A notification system gives you better latency with lower overhead, but it is probably also going to be more difficult to deploy, and will probably be overkill if this is just an app for internal use. Even with a notification system, you need to do some careful protocol design to be sure not to miss something.

这篇关于用Django和bokeh绘制实时数据的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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