程序化散景服务器

时间:2018-02-04 23:20:56

标签: python tornado bokeh

我是Bokeh的新手。我试图以编程方式运行Bokeh服务器,该服务器在PyQt应用程序中启动。

本文http://matthewrocklin.com/blog/work/2017/06/28/simple-bokeh-server描述了如何执行此操作,但它需要使用python笔记本,因为Tornado Web服务器搭载Jupyter笔记本自己的IOLoop。

我不想使用Jupyter笔记本,但我想运行它以便“捎带PyQt的io循环”。

我已尝试将服务器作为其独立的线程运行,但它不起作用(访问网站localhost:5001只是挂起说“等待localhost”。)

以下是我目前拥有的主题。当用户单击按钮时,从我的主PyQt应用程序中的QThreadPool调用它。

class Streamer(QtCore.QRunnable):

    def __init__(self):
        super(Streamer, self).__init__()

    def update(self):
        new = {'x': [random.random()],
               'y': [random.random()],
               'color': [random.choice(['red', 'blue', 'green'])]}
        self.source.stream(new)

    def make_document(self, doc):
        self.source = ColumnDataSource({'x': [], 'y': [], 'color': []})
        doc.add_periodic_callback(self.update, 100)

        self.fig = figure(title='Streaming Circle Plot!', sizing_mode='scale_width',
                     x_range=[0, 1], y_range=[0, 1])
        self.fig.circle(source=self.source, x='x', y='y', color='color', size=10)

        doc.title = "Now with live updating!"
        doc.add_root(self.fig)

    def run(self):
        self.apps = {'/': Application(FunctionHandler(self.make_document))}
        server = Server(self.apps, port=5001)
        server.start()
        server.show('/')
        while True:
            QtCore.QThread.sleep(1)

修改

我尝试使用server.run_until_shutdown()代替server.start()但我收到错误

  

信号仅适用于主线程

但我无法在我的主线程中运行散景服务器,因为它挂起了我的GUI应用程序。

1 个答案:

答案 0 :(得分:1)

我找到了解决这个问题的方法之一。

使用server.start()无法启动tornado网络服务器,因此初始方法不会发生任何事情。另一方面,查看server.run_until_shutdown()的代码会在运行tornado IOLoop`之前生成系统信号,如果服务器在线程上运行则无法生效。

我在bokeh存储库中找到了一个示例,并从docs链接,其中torando服务器可以在线程的阻塞调用中手动启动(无需信号)。这里的代码应该放在线程的run方法中:

def run(self):
  server = Server({'/': self.make_document})
  server.start()

  server.io_loop.add_callback(server.show, "/")
  server.io_loop.start()