如何停止Bokeh服务器?

9
我使用bokeh在本地局域网上实时绘制传感器数据。我会在我的python应用程序中使用popen启动bokeh:Popen("bokeh serve --host=localhost:5006 --host=192.168.8.100:5006", shell=True) 我想从应用程序中关闭bokeh服务器,但是我在文档中没有找到任何相关信息。同时bokeh serve --help也没有提供任何提示。
编辑:根据被接受的答案,我想出了以下解决方案:
        self.bokeh_serve = subprocess.Popen(shlex.split(command),
                             shell=False, stdout=subprocess.PIPE)

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

3个回答

4

如果你使用的是基于Linux的操作系统,那么打开终端并输入以下命令

ps -ef

查找正在运行的bokeh应用程序文件并记录PID即进程ID,假设进程ID为3366,则使用以下命令:

kill 3366

结束进程。

我会这样做: ps -ef | grep bokeh在这种情况下,只有与bokeh相关的进程ID将出现在终端中,然后执行 kill -9 <process id> - Amin Kiany

3

如果您不了解Bokeh并且假设您使用的是Python>=3.2或Linux,则可以尝试使用SIGTERMSIGINTSIGHUP来终止进程,使用os.kill()Popen.pid,甚至更好的是使用Popen.send_signal()。如果Bokeh具有适当的信号处理程序,它甚至会正常关闭。

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


3

我在我的Python 3.7Bokeh服务器编程中使用了一种非常简单的方式来停止服务器,没有显式的TornadoServer导入。我的操作系统是Windows 7,我使用Python 3.7,因为Python 3.8Bokeh服务器不兼容。

我从外部启动一个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"

你可以在默认的网络浏览器中看到这个。它会添加一个新的选项卡,并显示一个图表,其中有越来越多的小圆圈,直到按下停止按钮并看到派对结束。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接