简体   繁体   中英

Flask with gevent multicore

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.

I'll take a shot!

Nginx!

server section:

location / {
    include proxy_params;
    proxy_pass http://127.0.0.1:5000;
}

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

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

If you must have gevent you might like this little setup

config.py:

import multiprocessing

workers = multiprocessing.cpu_count()
bind = "127.0.0.1:5000"
worker_class = 'gevent'
worker_connections = 30

Then you can run:

gunicorn -c config.py myapp:app

Thats right you have a worker for each cpu and 30 connections per worker.

See if that works for you.

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;
} 

then one of these in the server section

location / {
    include proxy_params;
    proxy_pass http://backend;
}

Good Luck buddy!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM