使用CherryPy/Cherryd启动多个Flask实例 [英] Using CherryPy/Cherryd to launch multiple Flask instances

查看:175
本文介绍了使用CherryPy/Cherryd启动多个Flask实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据SO/SF和其他站点上的建议,我将CherryPy用作WSGI服务器来启动用Flask构建的Python Web服务器的多个实例.每个实例都在其自己的端口上运行,并位于Nginx的后面.我应该注意,以下内容确实对我有用,但令我感到困扰的是,我以错误的方式处理问题,并且它偶然"起作用.

Per suggestions on SO/SF and other sites, I am using CherryPy as the WSGI server to launch multiple instances of a Python web server I built with Flask. Each instance runs on its own port and sits behind Nginx. I should note that the below does work for me, but I'm troubled that I have gone about things the wrong way and it works "by accident".

这是我当前的cherrypy.conf文件:

Here is my current cherrypy.conf file:

[global]
server.socket_host = '0.0.0.0'
server.socket_port = 8891
request.dispatch: cherrypy.dispatch.MethodDispatcher()
tree.mount = {'/':my_flask_server.app}

在不深入我的Flask服务器的情况下,它是这样开始的:

Without diving too far into my Flask server, here's how it starts:

import flask
app = flask.Flask(__name__)

@app.route('/')
def hello_world():
    return "hello"

这是我在命令行上使用Cherryd启动的命令:

And here is the command I issue on the command line to launch with Cherryd:

cherryd -c cherrypy.conf -i my_flask_server

问题是:

  1. 是否将Flask包裹在CherryPy中仍是在生产中使用Flask的首选方法? https://stackoverflow.com/questions/4884541/cherrypy-vs-flask-werkzeug

这是使用.conf文件启动CherryPy并导入Flask应用程序的正确方法吗?我已经仔细阅读过CherryPy文档,但找不到任何与我在此处尝试的操作完全匹配的用例.

Is this the proper way to use a .conf file to launch CherryPy and import the Flask app? I have scoured the CherryPy documentation, but I cannot find any use cases that match what I am trying to do here specifically.

是在单个计算机上启动多个CherryPy/Flask实例的正确方法,以针对要使用的每个端口(8891、8892,等等)?还是有更好的"CherryPy"方式来实现这一目标?

Is the proper way to launch multiple CherryPy/Flask instances on a single machine to execute multiple cherryd commands (daemonizing with -d, etc) with unique .conf files for each port to be used (8891, 8892, etc)? Or is there a better "CherryPy" way to accomplish this?

感谢您的帮助和见识.

推荐答案

我不能说Flask,但是我可以说CherryPy.多数情况下,这看起来像是正确的方式".关于MethodDispatcher的那行是空话,因为它只会影响CherryPy应用程序,并且您似乎没有挂载任何文件(而是仅挂载了一个Flask应用程序).

I can't speak for Flask, but I can for CherryPy. That looks like the "proper way"...mostly. That line about a MethodDispatcher is a no-op since it only affects CherryPy Applications, and you don't appear to have mounted any (just a single Flask app instead).

关于第3点,您说对了. CherryPy允许您在同一进程中运行多个Server对象,以侦听多个端口(或协议),但是它没有启动多个进程的糖.如您所说,具有不同配置文件的多个cherryd命令是如何做到的(除非您想使用更集成的集群/配置管理工具,例如鸡蛋怪物).

Regarding point 3, you have it right. CherryPy allows you to run multiple Server objects in the same process in order to listen on multiple ports (or protocols), but it doesn't have any sugar for starting up multiple processes. As you say, multiple cherryd commands with varying config files is how to do it (unless you want to use a more integrated cluster/config management tool like eggmonster).

这篇关于使用CherryPy/Cherryd启动多个Flask实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆