问题描述
在gevent后端服务器上运行flask应用程序并利用所有处理器核心的明确方法是什么?我有想法运行烧瓶应用程序的多个副本,其中gevent WSGIServer监听diapason 5000..5003(用于4个进程)中的一个端口,并使用nginx作为负载均衡器.
What is the clear way to run flask application with gevent backend server and utilize all processor cores? I have idea to run multiple copies of flask application where gevent WSGIServer listen one port in diapason 5000..5003 (for 4 processes) and nginx as load balancer.
但是我不确定这种方法是最好的,可能还有其他方法可以做到.例如,主进程监听一个端口,而工作进程监听传入的连接.
But I'm not sure that this way is the best and may be there are some other ways to do it. For example, master process listen one port and workers process incoming connections.
推荐答案
我会开枪!
location / {
include proxy_params;
proxy_pass http://127.0.0.1:5000;
}
Flask应用程序
这是一个简单的烧瓶应用程序,我将在此示例中使用.
Flask App
This is a simple flask app that i will be using for this example.
myapp.py:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run()
uWSGI
好的,我知道您说过您想使用gevent,但是如果您愿意对此稍作折衷,我想您会对这种设置感到非常满意.
uWSGI
Okay so I know that you said that you wanted to use gevent, but if you are willing to compromise on that a little bit I think you would be very happy with this setup.
[uwsgi]
master = true
plugin = python
http-socket = 127.0.0.1:5000
workers = 4
wsgi-file = myapp.py
callable = app
Gunicorn
如果您必须拥有gevent,则可能需要此小设置
Gunicorn
If you must have gevent you might like this little setup
config.py:
config.py:
import multiprocessing
workers = multiprocessing.cpu_count()
bind = "127.0.0.1:5000"
worker_class = 'gevent'
worker_connections = 30
然后您可以运行:
gunicorn -c config.py myapp:app
没错,每个cpu都有一个工作人员,每个工作人员有30个连接.
Thats right you have a worker for each cpu and 30 connections per worker.
看看是否适合您.
如果您真的将nginx用作负载平衡器,请尝试在http部分中尝试类似的操作
If you are really sold on using nginx as a load balancer try something like this in your http section
upstream backend {
server 127.0.0.1:5000;
server 127.0.0.1:5002;
server 127.0.0.1:5003;
server 127.0.0.1:5004;
}
然后在服务器部分中
location / {
include proxy_params;
proxy_pass http://backend;
}
祝你好运!
这篇关于带gevent多核的烧瓶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!