Flask with gevent multicore - python

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!

Related

Can not reach Flask app served with uWSGI and Nginx

I have a Flask back end that is functional without using uwsgi and nginx.
I'm trying to deploy it on an EC2 instance with its front-end.
No matter what I do, I can't reach the back-end. I opened all the ports for testing purposes but that does not help.
Here's my uwsgi ini file:
[uwsgi]
module = main
callable = app
master = true
processes = 1
socket = 0.0.0.0:5000
vacuum = true
die-on-term = true
Then I use this command to start the app:
uwsgi --ini uwsgi.ini
The message returned is
WSGI app 0 (mountpoint='') ready in 9 seconds.
spawned uWSGI worker 1 (and the only) PID: xxxx
Then here is my Nginx conf file:
server {
server_name my_name.com www.ny_name.com
location / {
root /home/ubuntu/front_end/dist/;
}
location /gan {
proxy_pass https:localhost:5000/gan;
}
## below https conf by certbot
}
If my understanding is correct, whenever a request reaches "my_name.com/gan..." it will be redirected to the localhost on the port 5000 where the back-end is started by uwsgi.
But I can't reach it. I'm trying to simply do a get request on "my_name.com/gan" on my browser (it should return a random image) but I get a 502 by nginx.
Important to note, the front-end works fine and I can access it on browser.
My guess is that url is not in proper form
Try
proxy_pass http://0.0.0.0:5000;

Running a Flask app on nginx

I don't know exactly what I am doing but I am experimenting with running Flask on nginx. I am boiling it down to the simple bit of code below. First I have a test app in Flask like this:
from flask import Flask, render_template
app = Flask(__name__, static_folder='client', template_folder='client/html')
def show_home_page():
return render_template("home.html")
#app.route('/')
def server():
return show_home_page()
if __name__ == '__main__':
app.run(threaded=True)
If I run python app.py I can go to http://localhost:5000 and see the "Hello World". Next I read that I need to run uwsgi but its not clear what params I need to pass to it. I tried different things like:
uwsgi -s /tmp/app.sock --manage-script-name --mount ./=app:app
I also noted that I need to set my nginx conf file to match, but I am stuck on that as well (I just get a welcome from nginx on port 5000) and it doesnt seem to link with my Flask app. I googled around a bit but nothing has clicked yet.
server {
listen 5000;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
You can use refer this for Flask with Nginx and uWSGI:
Python flask with Nginx and uWSGI

running uwsgi as master won't handle requests

I have the following set up
src
|
|--flask_app
|--|--controllers.py
|--|--provider.py
|--|--__init__.py
|--config.py
|--wsgi.py
|--myproject.ini
|--myproject.sock
init.py creates the flask application
from flask import Flask, g
from flask_app.controllers import api_module
from flask_app.provider import CassandraDbProvider
# Define WSGI application object
app = Flask(__name__)
# Configure it
app.config.from_object('config')
cassandra = CassandraDbProvider(**app.config['DATABASE_CONNECT_OPTIONS'])
#app.before_request
def before_request():
g.db = cassandra
app.register_blueprint(api_module)
controler.py has the views that run on endpoints and also creates the blueprint
Finally wsgi.py has the following code
from cassandra_flask_app import app as application
if __name__ == "__main__":
application.run(host="0.0.0.0", port=5000)
myproject.ini
[uwsgi]
module = wsgi
logto = /var/log/uwsgi/uwsgi.log
threads = 10
socket = myproject.sock
chmod-socket = 664
vacum = true
die-on-term = true
Upstart script
description "uWSGI server instance configured to serve myproject"
start on runlevel [2345]
stop on runlevel [!2345]
setuid myuser
setgid www-data
env PATH=/home/myuser/myproject/myprojectenv/bin
chdir /home/myuser/myproject/src
exec uwsgi --ini myproject.ini
and nginx
server {
listen 80;
server_name myIpHere;
location / {
include uwsgi_params;
uwsgi_pass unix:/home/myuser/myproject/src/myproject.sock;
}
}
controller.py
api_module = Blueprint('my_api', __name__, prefix='/api') # this might be wrong now because I don't have code infront of me
#myvmip/api/ works fine when uwsgi is master
#api_module.route('/', methods=['GET']):
def test_url():
return 'c'
#myvmip/api/normal_view/?query1=some_value" doesn't work when in master with no error. Only connection timeout error in nginx error.log.
#api_module.route('/nomral_view/', methods=['GET'])
def normal_view():
get_parameter = request.args.get('query1')
#uses g.db to connect to database and fetch some results
#database is cassandra db
return jsonify(**json)
It runs good on my developement machine. On my vm I load my flask application using nginx and uwsgi. I have set up uwsgi as described on many tutorial on the internet. The problem is that If I run uwsgi as master then It won't access some of my urls. Disabling it works properly. What do you think it could be? Does it matter uwsgi isn't loaded as master?
You need to add the following to your myproject.ini file
chdir = /path/to/project
callable = application
http://uwsgi-docs.readthedocs.org/en/latest/WSGIquickstart.html#deploying-flask
Also if you have a virtualenv you should add
venv=/path/to/venv

BottlePy App and CherryPy server not logging access

I'm serving a BottlePy App with CherryPy like this:
import cherrypy
from myapp import MyApp
from beaker.middleware import SessionMiddleware
appdir = '/path/to/app'
app = MyApp(appdir)
session_opts = {
'session.timeout': 600, # 10 minutes
'session.type': 'file',
'session.auto': True,
'session.data_dir': appdir + '/auth/data'
}
app = SessionMiddleware(app, session_opts)
cherrypy.tree.graft(app, '/')
cherrypy.config.update({
'log.screen': False,
'log.access_file': appdir + '/front_end/cherrypy.access.log',
'log.error_file': appdir + '/front_end/cherrypy.error.log',
'server.socket_port': 8080,
'server.socket_host': '0.0.0.0'
})
cherrypy.engine.start()
cherrypy.engine.block()
Everything seems to be working properly, but cherrypy.access.log remains totally empty, while cherrypy.error.log reads:
[30/Dec/2014:11:04:55] ENGINE Bus STARTING
[30/Dec/2014:11:04:55] ENGINE Started monitor thread '_TimeoutMonitor'.
[30/Dec/2014:11:04:55] ENGINE Started monitor thread 'Autoreloader'.
[30/Dec/2014:11:04:56] ENGINE Serving on http://0.0.0.0:8080
[30/Dec/2014:11:04:56] ENGINE Bus STARTED
But nothing else, no access logs, even after serving content.
I also tried
from cherrypy import wsgiserver
# Instead of the cherrypy.* calls
server = wsgiserver.CherryPyWSGIServer(('0.0.0.0', 8080), app)
server.start()
but it will print the same as above, but no access logs. Could not find any further documentation on logging and BottlePy integration.
The app you're running is not native CherryPy one, and grafting basically bypasses most of CP's internals, most likely including access logging.
Since you don't use any CherryPy's features besides basic WSGI publishing, you may be better off using one of the more serving-oriented (and more recent) solutions like uWSGI, Gunicorn or nginx/Apache+plugins.
You need to set log.screen to True to enable both error and access logs.
See (old) docs: https://cherrypy.readthedocs.org/en/3.3.0/refman/_cplogging.html

Serve a Flask app on a sub-url during localhost development

I have a Flask app as backend that serves a REST API and an AngularJS front-end app.
I use Grunt/Livereload to serve the front-end at the address: http://localhost:5000/
Is it possible to serve the Flask app on a sub-url of localhost during development, using app.run() or run_simple from werkzeug?
Specifically I would like to have the Flask app accessible at the address: http://localhost:5000/api
I found this solution but it has the disadvantage of serving a dummy app at http://localhost:5000/ which uses the address and doesn't let me serve the AngularJS app at that address.
The way that Flask and Yoman are set up you cannot actually do this - it is possible to have two processes share the same port, but it is generally done to allow one master process to pass off handling of individual requests to sub-processes, which is not quite what you are doing here. (In general, in production, you would run both the front and back end behind a proxy server like nginx.)
Fortunately, you do not have to re-invent the wheel or run a separate proxy server just to develop your app - there is a Grunt plugin called grunt-connect-proxy that will let you proxy requests to a sub-url to another location entirely. That will let you spin up your Flask backend server on a different port (say port 5001) and proxy requests to localhost:5000/api (for example) to localhost:5001/:
connect: {
options: {
port: 5000,
hostname: 'localhost'
},
proxies: [
{
context: '/api',
host: '127.0.0.1',
port: 5001,
https: false,
changeOrigin: false,
xforward: false
}
]
}
And then you can run your Flask app with app.run(port=5001).
This worked for me
from flask import Flask
prefix = '/abc'
app = Flask(__name__)
# redefine route
def route(path, *args, **kwargs):
return _route(prefix + path, *args, **kwargs)
_route = app.route
app.route = route
# Test function
#app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run()

Categories

Resources