9

I do use bokeh to plot sensor data live on the local LAN. Bokeh is started from within my python application using popen: Popen("bokeh serve --host=localhost:5006 --host=192.168.8.100:5006", shell=True)

I would like to close bokeh server from within the application. However, I cannot find anything in the documentation. Also bokeh serve --help does not give any hint how to do that.

EDIT: based on the accepted answer I came up with following solution:

        self.bokeh_serve = subprocess.Popen(shlex.split(command),
                             shell=False, stdout=subprocess.PIPE)

I used self.bokeh_serve.kill() for ending the process. Maybe .terminate() would be better. I will try it.

Moritz
  • 5,130
  • 10
  • 40
  • 81

3 Answers3

4

if you are using Linux based OS then open terminal and type

ps -ef

Search for the bokeh application file running in the process and note down the PID i.e. Process ID, suppose process ID id 3366 then using command

kill 3366

kill the process.

  • I would do like this: ```ps -ef | grep bokeh``` In this case, only processed id related to bokeh will be appeared in Terminal and then ```kill -9 ```. – Amin Kiany May 11 '22 at 09:27
3

Without knowing bokeh and assuming that you use Python >= 3.2 or Linux, you could try to kill the process with SIGTERM, SIGINT or SIGHUP, using os.kill() with Popen.pid or even better Popen.send_signal(). If bokeh has proper signal handlers, it will even shutdown cleanly.

However, you may be better off using the option shell=False, because with shell=True, the signal is sent to the shell instead of the actual process.

code_onkel
  • 2,759
  • 1
  • 16
  • 31
3

There is a very simple way that I use in my Python 3.7 with Bokeh server programming to stop the server, with no explicit Tornado or Server imports. My OS is Windows 7 and I use Python 3.7 because Python 3.8 is not compatible with Bokeh server

I start a bokeh server from outside with

bokeh serve --show myprogr.py

The content of myprog.py:

import numpy as np
from bokeh.plotting import figure, curdoc
from bokeh.models.widgets import Button
from bokeh.layouts import column, widgetbox
import sys

def button_callback():
    sys.exit()  # Stop the server

def add_circles():
    # 10 more circles
    sample_plot.circle(x=np.random.normal(size=(10,)),
                       y=np.random.normal(size=(10,)))

bokeh_doc = curdoc()
sample_plot = figure(plot_height=400, plot_width=400) # figure frame

# Button to stop the server
button = Button(label="Stop", button_type="success")
button.on_click(button_callback)

bokeh_doc.add_root(column([sample_plot, widgetbox(button, align="center")]))
bokeh_doc.add_periodic_callback(add_circles, 1000)
bokeh_doc.title = "More and more circles with a button to stop"

One can see in your default web browser. It adds a new tab and shows a graph with a growing number of small circles until press stop and see that the party is over.

Paulo Buchsbaum
  • 2,471
  • 26
  • 29