Search code examples
pythonbokeh

Run multiple Bokeh servers


I have multiple Python scripts to create Bokeh apps on different ports.

Currently, I start each server individually from command line using bokeh serve

What is the easiest way to run multiple Bokeh servers on different ports ideally by running a single Python script?

Is it possible to set the permitted clients individually for each server?


Solution

  • A bash solution would be simpler, but if you really need a Python one then here it is:

    import shlex
    from multiprocessing import Process
    
    from bokeh.command.bootstrap import main
    
    argss = ['--port 57878 --allow-websocket-origin localhost:57878 test.py',
             '--port 58989 --allow-websocket-origin localhost:58989 test.py']
    
    processes = [Process(target=main, args=(['python', 'serve'] + shlex.split(args),)) for args in argss]
    for p in processes:
        p.start()
    for p in processes:
        p.join()
    

    Just adapt the argss list to your needs.

    Note that bokeh.command.boostrap is not a public API - it may easily change in any future version. To prevent breakage, you can use the subprocess module to instead start proper bokeh serve commands.