Example on embedding Bokeh Server inside ASGI Starlette app

Hi everyone,

As we are increasingly using the Bokeh Server at our company, I decided to “standardize” all the Bokeh Server deployments inside our data science team.

Given that we love deploying web apps using Gunicorn (because it just works and it’s super-easy to manage), I was looking for a way of deploying with Gunicorn but without losing all async capabilities end-to-end.

Fortunately, ASGI (which is basically WSGI + native async support) is becoming increasingly popular, and most modern Python async web frameworks (Starlette, Sanic, Django Channels…) are embracing its usage.

Unfortunately Tornado (what Bokeh Server is based on) does not support ASGI deployments. Nevertheless, it is possible to embed the Bokeh Server inside one of these ASGI frameworks to have a fully ASGI async pipeline.

I have prepared an example here on how to embed the Bokeh Server inside an Starlette app, using Uvicorn (which is Gunicorn + ASGI support) to deploy. The nice thing is that both the Bokeh Server and the rest of the Starlette app share the same ioloop, which in fact can be written in C (such as uvloop)

Hope someone finds it interesting for wide production deployments!

1 Like

@julioasotodv First off thank you for sharing! This is really great to see explorations in this direction.

I’d actually like very much to “lift” the Bokeh protocol more out on its own, so that it could be used in e.g. by ASGI application, without the Tornado Bokeh server. Or in other words, I’d like the Tornado-based Bokeh server to become more of a “reference implementation” rather than the implementation of a Bokeh protocol server. My current knowledge is still fairly limited, though. If you are interested to collaborate on this, I’d love to get your help/guidance!