How to get bokeh server to start and stop when a user goes to a web page

Hey All,

Right now I’m using the standard method of running a bokeh server, where each new user going to the web page gets their own instance of the bokeh app. But my bokeh app uses hardware attached to the computer, so there can only be 1 instance running at a time. I also want it to be able to stop (or have a special web page you go to that stops it) so a user can “leave” and another user can start the app. I know there’s no way to know when a user closes a web page, that’s why I figure I will need the special page.

So should I not use the application factory (see below), since I only want there to be one instance available at any time?

Right now I’m doing the following, which works, but when the first user leaves the page the bokeh server keeps running and I want it to be able to stop and restart:

bokeh_app = Application(FunctionHandler(main_doc))

hosts = [’*’]

sockets = netutil.bind_sockets(port=5006, address=None)

BokehTornado_app = BokehTornado({’/bkapp’: bokeh_app}, extra_websocket_origins=hosts)

http_server_kwargs = {‘xheaders’: False}
http_server_bokeh = HTTPServer(BokehTornado_app, **http_server_kwargs)
http_server_bokeh.start()
http_server_bokeh.add_sockets(sockets)

bokeh_server = BaseServer(io_loop, BokehTornado_app, http_server_bokeh)
bokeh_server.start()
script = server_document(url=‘http://0.0.0.0:5006/bkapp’)
return render_template(‘bokeh_app.html’, script=script)

Thanks!

Hi,

There's not really anything in the standard Bokeh machinery to support anything like "single user at a time". To support that with the standard server classes would require presenting any additional connections from every reaching the Bokeh server in the first place (maybe the web app or a proxy like Nginx could take care of that, I don't know for sure). Otherwise I think you are looking at subclassing/patching/rewriting chunks of the bokeh.server module to support limiting connections in the way.

Another options is to let all connections in, but have the app code behave differently based on some lock/semaphore that indicates whether the hardware can be acquired for use. Then the app simply adds one Div that displays "Currently in use, try back later" or whatever instead of the application does when it can actually run. Of course, you might have to contend with race conditions between checking and acquiring the lock to be really robust.

Thanks,

Bryan

···

On Jun 20, 2018, at 10:30, [email protected] wrote:

Hey All,

Right now I'm using the standard method of running a bokeh server, where each new user going to the web page gets their own instance of the bokeh app. But my bokeh app uses hardware attached to the computer, so there can only be 1 instance running at a time. I also want it to be able to stop (or have a special web page you go to that stops it) so a user can "leave" and another user can start the app. I know there's no way to know when a user closes a web page, that's why I figure I will need the special page.

So should I not use the application factory (see below), since I only want there to be one instance available at any time?

Right now I'm doing the following, which works, but when the first user leaves the page the bokeh server keeps running and I want it to be able to stop and restart:

bokeh_app = Application(FunctionHandler(main_doc))
hosts = ['*']
sockets = netutil.bind_sockets(port=5006, address=None)
BokehTornado_app = BokehTornado({'/bkapp': bokeh_app}, extra_websocket_origins=hosts)
http_server_kwargs = {'xheaders': False}
http_server_bokeh = HTTPServer(BokehTornado_app, **http_server_kwargs)
http_server_bokeh.start()
http_server_bokeh.add_sockets(sockets)
bokeh_server = BaseServer(io_loop, BokehTornado_app, http_server_bokeh)
bokeh_server.start()
script = server_document(url='http://0.0.0.0:5006/bkapp'\)
return render_template('bokeh_app.html', script=script)

Thanks!

--
You received this message because you are subscribed to the Google Groups "Bokeh Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/f5fcd23c-8c4f-45bb-8586-12cda0483dc6%40continuum.io\.
For more options, visit https://groups.google.com/a/continuum.io/d/optout\.

Ok, that clears up a lot. I think my best bet is to leave the server running but change what it does based on a semaphore, like you said. Thanks!

-Marc

···

On Wednesday, June 20, 2018 at 6:55:37 PM UTC-4, Bryan Van de ven wrote:

Hi,

There’s not really anything in the standard Bokeh machinery to support anything like “single user at a time”. To support that with the standard server classes would require presenting any additional connections from every reaching the Bokeh server in the first place (maybe the web app or a proxy like Nginx could take care of that, I don’t know for sure). Otherwise I think you are looking at subclassing/patching/rewriting chunks of the bokeh.server module to support limiting connections in the way.

Another options is to let all connections in, but have the app code behave differently based on some lock/semaphore that indicates whether the hardware can be acquired for use. Then the app simply adds one Div that displays “Currently in use, try back later” or whatever instead of the application does when it can actually run. Of course, you might have to contend with race conditions between checking and acquiring the lock to be really robust.

Thanks,

Bryan

On Jun 20, 2018, at 10:30, mar…@vt.edu wrote:

Hey All,

Right now I’m using the standard method of running a bokeh server, where each new user going to the web page gets their own instance of the bokeh app. But my bokeh app uses hardware attached to the computer, so there can only be 1 instance running at a time. I also want it to be able to stop (or have a special web page you go to that stops it) so a user can “leave” and another user can start the app. I know there’s no way to know when a user closes a web page, that’s why I figure I will need the special page.

So should I not use the application factory (see below), since I only want there to be one instance available at any time?

Right now I’m doing the following, which works, but when the first user leaves the page the bokeh server keeps running and I want it to be able to stop and restart:

bokeh_app = Application(FunctionHandler(main_doc))

hosts = [‘*’]

sockets = netutil.bind_sockets(port=5006, address=None)

BokehTornado_app = BokehTornado({‘/bkapp’: bokeh_app}, extra_websocket_origins=hosts)

http_server_kwargs = {‘xheaders’: False}

http_server_bokeh = HTTPServer(BokehTornado_app, **http_server_kwargs)

http_server_bokeh.start()

http_server_bokeh.add_sockets(sockets)

bokeh_server = BaseServer(io_loop, BokehTornado_app, http_server_bokeh)

bokeh_server.start()

script = server_document(url=‘http://0.0.0.0:5006/bkapp’)

return render_template(‘bokeh_app.html’, script=script)

Thanks!


You received this message because you are subscribed to the Google Groups “Bokeh Discussion - Public” group.

To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].

To post to this group, send email to [email protected].

To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/f5fcd23c-8c4f-45bb-8586-12cda0483dc6%40continuum.io.

For more options, visit https://groups.google.com/a/continuum.io/d/optout.