Bokeh Session and Poll of Documents

I am trying to serve bokeh documents through Django using the bokeh-server executable that creates an instance of Tornado. You can access bokeh documents at the URL provided by the Session.object_link method. When navigating, the bokeh server executable writes this to stdout (IP addresses have been replaced by ellipses):

INFO:tornado.access:200 POST /bokeh/bb/71cee48b-5122-4275-bd4f-d137ea1374e5/gc (...) 222.55ms INFO:tornado.access:200 GET /bokeh/bb/71cee48b-5122-4275-bd4f-d137ea1374e5/ (...) 110.15ms INFO:tornado.access:200 POST /bokeh/bb/71cee48b-5122-4275-bd4f-d137ea1374e5/gc (...) 232.66ms INFO:tornado.access:200 GET /bokeh/bb/71cee48b-5122-4275-bd4f-d137ea1374e5/ (...) 114.16ms 

This seems to be the connection between the python instance launching the Django WSGI application (initialized by Apache executing mod_wsgi) and the bokeh server executable.

When the browser sends a response, including the graphs and data, etc. needed for the bokeh interface, the browser has some initial network, and then the network if there is any interaction with the graphs that have python callbacks. When a user closes a window or browser, the same network continues to work. In addition, the network stops only when destroying Django processes or a bokeh server.

To start a bokeh session and pass the URL back to the Django template, you need to start the bokeh session in a new stream:

 def get_bokeh_url(self, context): t = Thread(target=self.run) t.start() return self.session.object_link(self.document.context) def run(self): return self.session.poll_document(self.document) 

self.session and self.document were initialized before the start of the stream. So, at the time get_bokeh_url is called, the document has some graphs, some of which have interaction callbacks and a session, but are not polled through poll_document (which seems necessary for interaction).

A thread continues to work forever unless you destroy a Django or bokeh server. This means that when more requests appear, the number of threads increases and the number of networks increases.

My question is, is there a way to kill the stream as soon as the document is no longer viewed in the browser?

One of the answers I thought about was to send a quick request to the server when the browser closes and somehow kills the stream for this document. I tried to delete documents from the bokeh interface, but this has no effect.

+5
source share
1 answer

The bokeh server periodically checks for connections to the session. If for some time there have been no connections, the session has expired and is destroyed.

Starting with version 0.12.1, the verification interval and the maximum connection establishment time by default are 17 and 60 seconds, respectively. You can override them by starting the server this way

 bokeh serve --check-unused-sessions 1000 --unused-session-lifetime 1000 app.py 

This is quite difficult to find in the documents described in the CLI documentation and in the developer's guide, in the "Applications, Sessions and Connections" section of the " Server Architecture" chapter. There is also a Github closed issue on this topic: Periodic callbacks continue after tabs are closed # 3770

If you need custom logic whenever a session is destroyed, use the deployment directory for your application and add the server_lifecycle.py file containing your Lifecycle Hooks , in particular this one:

 def on_session_destroyed(session_context): ''' If present, this function is called when a session is closed. ''' pass 
+2
source

Source: https://habr.com/ru/post/1215453/


All Articles