如何停止散景服务器?

How to stop bokeh server?

我确实使用 bokeh 在本地 LAN 上实时绘制传感器数据。 Bokeh 是在我的 python 应用程序中使用 popen 启动的:Popen("bokeh serve --host=localhost:5006 --host=192.168.8.100:5006", shell=True)

我想从应用程序中关闭散景服务器。但是,我在 documentation 中找不到任何内容。 bokeh serve --help 也没有给出任何提示如何做到这一点。

编辑:根据接受的答案,我提出了以下解决方案:

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

我使用 self.bokeh_serve.kill() 来结束进程。也许 .terminate() 会更好。我试试看。

不知道散景并假设您使用 Python >= 3.2 或 Linux,您可以尝试使用 SIGTERMSIGINT 或 [= 终止进程12=],使用 os.kill() with Popen.pid or even better Popen.send_signal()。如果散景有适当的信号处理程序,它甚至会干净地关闭。

但是,您最好使用选项 shell=False,因为使用 shell=True,信号会发送到 shell 而不是实际进程。

如果您使用的是基于 Linux 的 OS,则打开终端并输入

ps -ef

在进程中搜索bokeh应用程序文件运行并记下PID即进程ID,假设进程ID id 3366然后使用命令

kill 3366

终止进程。

我在 Python 3.7Bokeh 服务器编程中使用了一个非常简单的方法来停止服务器,没有明确的 TornadoServer 导入。我的 OS 是 Windows 7 我使用 Python 3.7 因为 Python 3.8 不兼容 Bokeh 服务器

我从外部启动一个 bokeh 服务器

bokeh serve --show myprogr.py

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"

可以在您的默认网络浏览器中看到。它添加了一个新选项卡并显示了一个带有越来越多小圆圈的图表,直到按下 stop 并看到聚会结束。