多个散景服务器集成到Flask

问题描述 投票:0回答:1

我试图在烧瓶应用程序中运行多个Bokeh服务器,这些图使用如下方法正确运行:

  def getTimePlot():
   script = server_document('http://localhost:5006/timeseries')
   return render_template("displaytimeseries.html", script=script, template="Flask")    
def startPlotserver():
    server.start()
    server = Server({'/timeseries': modifyTimeSeries}, io_loop=IOLoop(), allow_websocket_origin=["localhost:8000"])
    server.io_loop.start() 
if __name__ == '__main__':
    print('Opening single process Flask app with embedded Bokeh application on http://localhost:8000/')
    print()
    print('Multiple connections may block the Bokeh app in this configuration!')
    print('See "flask_gunicorn_embed.py" for one way to run multi-process')
    app.run(port=5000, debug=True)

但是当我尝试使用这种方法将两台服务器嵌入到烧瓶中时,我就会遇到问题:

文件结构:

|--app4
    |---webapp2.py
    |---bokeh
          |--timeseries.py
          |--map.py

我想我已经在这里找到了解决方法Link To Question我现在尝试使用提到的类似方法将地图服务器导入flak并最终得到如下内容:

1.文件构建器(不确定它为什么不捡起它)

def build_single_handler_applications(paths, argvs=None):
applications = {}
argvs = {} or argvs
for path in paths:
    application = build_single_handler_application(path, argvs.get(path, []))
    route = application.handlers[0].url_path()
    if not route:
        if '/' in applications:
            raise RuntimeError("Don't know the URL path to use for %s" % (path))
    route = '/'
    applications[route] = application
return applications

2.查找文件和创建连接的代码

    files=[]
for file in os.listdir("bokeh"):
    if file.endswith('.py'):
        file="map"+file
        files.append(file)

argvs = {}
urls = []
for i in files:
    argvs[i] = None
    urls.append(i.split('\\')[-1].split('.')[0])
host = 'http://localhost:5006/map'

apps = build_single_handler_applications(files, argvs)

bokeh_tornado = BokehTornado(apps, extra_websocket_origins=["localhost:8000"])
bokeh_http = HTTPServer(bokeh_tornado)
sockets, port = bind_sockets("localhost:8000", 5000)
bokeh_http.add_sockets(sockets)

3.调用服务器并呈现模板的代码

    @app.route('/crimeMap', methods=['GET'])
def getCrimeMap():
    bokeh_script = server_document("http://localhost:5006:%d/map" % port) 
    return render_template("displaymap1.html", bokeh_script=bokeh_script)

我在这样的单一命令中运行我的两个Bokeh服务器

bokeh serve timeseries.py map.py --allow-websocket-origin=127.0.0.1:5000

但是当我运行webapp2.py时,我收到此错误:

    (env1) C:\Users\Dell1525\Desktop\flaskapp\env1\app4>webapp2.py
Traceback (most recent call last):
  File "C:\Users\Dell1525\Desktop\flaskapp\env1\app4\webapp2.py", line 113, in <module>
    apps = build_single_handler_applications(files, argvs)
  File "C:\Users\Dell1525\Desktop\flaskapp\env1\app4\webapp2.py", line 29, in build_single_handler_applications
    application = build_single_handler_application(path, argvs.get(path, []))
NameError: name 'build_single_handler_application' is not defined

我发现并添加了来自Bokeh docs的build_single_handler_application函数只是因为这个错误所以我不确定它是否甚至是必需的还是正确的。我想知道我错过了什么,以防万一它是定位错误或缺少导入我附加完整烧瓶webapp2.py代码在这里:

Full Code

非常感谢你的帮助

python flask tornado bokeh
1个回答
0
投票

我通过稍微调整这个例子找到了一个更简单的解决方案:Link To Original Post注意这需要你有龙卷风4.4.1,因为它不适用于较新的版本

技巧是单独运行所有服务器,并在具有相同套接字访问权限的不同端口上运行

  bokeh serve timeseries.py --port 5100 --allow-websocket-origin=localhost:5567

bokeh serve map.py --port 5200 --allow-websocket-origin=localhost:5567

对于那些可能发现这个有用的人我已经包括完整的工作解决方案Link To Working Code

© www.soinside.com 2019 - 2024. All rights reserved.