I'm developing a web application with Python Flask.
I read something about WSGI server and the warning message "WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead."
If I'm using GKE or App Engine or Cloud Ran, is WSGI something I need to learn?
TLDR: No, you don't. You just need to know how to run your App with Flask. Having said that, it doesn't mean that learning/understanding WSGI is a waste but it isn't required.
Longer response
Gunicorn is a Production WSGI Webserver and this is what Google uses on their Production server to run your Apps for Google App Engine (if your App doesn't contain an entrypoint). Waitress is another Production WSGI webserver.
You don't necessarily have to 'learn' how to use any of them or the intricacies of WSGI to be able to build an App. Learning and understanding how Flask works is good enough
For Google App Engine
Just build and test your App on your dev environment with Flask (e.g. run your app with flask run main.py. When you deploy your App to Google App Engine, it will be run with Gunicorn (unless you specified an entrypoint that doesn't use gunicorn)
If on the other hand you use dev_appserver.py to run your app locally e.g. you run your app with dev_appserver.py app.yaml, gcloud CLI will first install gunicorn and then use it to run your App on your local machine.
In both of these instances, you don't have to be an expert on WSGI or gunicorn. Just knowing enough to run your app with Flask is what you need.
However, note that you can't run Python 3 Apps locally with dev_appserver.py on a Windows machine (see google documentation). I believe it's because gunicorn doesn't run on Windows. But if you still want to use dev_appserver.py for Python 3 Apps on a Windows machine, you can check out a Patch we created (the patch essentially swaps out Gunicorn for Waitress when running your App on your dev machine)
For Cloud Run
You can code and test your App with Flask and then use gunicorn in the container (you don't necessarily have to be an expert or know a lot about gunicorn). See 'hello world' sample application from Google
Related
I have develop python flask application(REST API). Now I want to deploy this application on client system(Windows 10 Professional ). My client dont have any internet service.
Previously, I done in java that time I make a .war file and deployed in tomcat on client system. He was able to access REST API.
Now I want know any similar way to deploy python app on client system, on start system his able to access my REST API
use PyInstaller.
pip install pyinstaller
go to project dir
cd C:\Users\sandip\Desktop\MyPython
use
pyinstaller --onefile HelloFlask.py
If you just want to make your rest APIs accessible by other users in same network, you can simply do it without installing anything on client side by replacing the app.run() in your code to app.run(host= '0.0.0.0'). By default flask app runs on localhost, by changing it to latter causes it to run on your machines IP address, thus making it accessible by all the users under same network. You can read more on flask's documentation under the heading Externally Visible Server.
To deploy your app in production, you need a WSGI server, you can read about deployment of flask app here
I am building a back end in python via the python flask application from IBM Cloud/Bluemix. I have heard/read a lot about people complaining regarding that Flasks built in server isn’t good for production. But how do I know if the application uses the Flask built in server or if IBM sets something else? Is there a simple way to see this in the code?
Deploying the Flask boilerplate app from the IBM cloud catalogue will indeed deploy a Flask application running on the Flask dev webserver.
You will need to alter the application if you want to run a production WSGI server.
I work for IBM and am in this stuff all day every day.
If you want to verify this, SSH into your application container on Cloud Foundry with the bash command
cf ssh <yourappnamehere>
You will need to have either the bluemix or cloud foundry CLIs installed and be logged in to the relevant endpoint before submitting this command.
It will open a bash shell in your application container, and you can cd around and open and/or download your project files for inspection.
This line:
app = Flask(__name__)
is a sure fire way to know that you are running a Flask web server application.
If you are concerned with which WSGI server your application is running under, checking your procfile (you should see this when SSHing int your container) will show you which command starts your application. If the command is
python <yourapp>.py
then you are running the dev server. Otherwise, you would be running some other python file, most likely via the server's command rather than the python command, that would import your application as a dependency.
You can also take a look at whether or not any WSGI server libraries were downloaded during the compilation of your droplet, and what command was used to start your application with
cf logs <yourappname> --recent
after deploying it.
Or, you can just believe me that the boilerplate deploys a Flask app under a Flask dev server.
A tutorial on running Flask on a different WSGI server:
https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uwsgi-and-nginx-on-ubuntu-14-04
I have an Linux instance running on Google Compute Engine. I installed pip and django on it and cloned a Django project that I worked on locally. Like I would on localhost I ran my app like so: python3 manage.py runserver 0.0.0.0:8080, and my server was up and running with no problems. I read online on how WSGI servers are required for python apps to run well on servers however I don't see why I would need something like gunicorn to run my app
Here's what the documentation for runserver says:
DO NOT USE THIS SERVER IN A PRODUCTION SETTING. It has not gone through security audits or performance tests. (And that’s how it’s gonna stay. We’re in the business of making Web frameworks, not Web servers, so improving this server to be able to handle a production environment is outside the scope of Django.)
Django's runserver is itself a WSGI server, but it's aimed at being easy for developers to use.
Production WSGI servers like uWSGI and Gunicorn have performance and production environments in mind. They handle concurrency better, they are faster, and are built to withstand malicious users, not just developers.
I am trying to run a Flask app locally which also needs to access resources run on a separate Flask app deployed on a remote server on the local network. I thought this may be possible using a DispatcherMiddleware layer locally, so, based on examples from:
http://flask.pocoo.org/docs/0.12/patterns/appdispatch/
How to implement Flask Application Dispatching by Path with WSGI?
Both examples require the DispatcherMiddleware layer (running locally) to have access to the constituent apps (which may be on a remote server) for example:
from app import app as app1
from app2.app import app as app2
from app3.app import app as app3
application = DispatcherMiddleware(app1, {
'/app2': app2, ##may be remote
'/app3': app3 ##may be remote
})
Is there any way to achieve this pattern given the distributed apps, short of creating a network share mounting the path to the remote server and importing over a network share?
DispatcherMiddleware is for serving multiple WSGI applications (like Flask) with one WSGI server (like Gunicorn). The WSGI server runs the app(s), the web server (like Nginx) passes requests to the WSGI server.
If your apps are distributed, then they would be run on their own machines. A WSGI server (and software in general) can't run things on other machines. Using DispatcherMiddleware makes no sense for that.
I built a small web app for a friend. That friend's computer will not be connected to the Internet when using the app, so deploying it on Heroku is not an option.
Is there a way to deploy it locally without having to install a complex web server? Something small that can be packaged with the application? Using the built-in Flask server seems to be discouraged when you go to "production", but for a local app is it ok?
If it's just going to be used offline by one person, then yes, the internal development server might be sufficient.
If you're looking for a simple way to send that app to her, then see pyinstaller:
pip install pyinstaller
pyinstaller your_app.py
Zip up the folder inside the new dist directory and pass that along.
If pyinstaller isn't for you, there are plenty of options.
If you're just running the app locally, it should be fine. The main issues with the dev server are security and performance, but for an app that's not exposed to the outside and that has a single user, it should work fine. Even though you're using the dev server, it's still a good idea to turn off debug mode and enable multiprocess mode.
from multiprocessing import cpu_count
app.run(debug=False, processes=cpu_count())
If you want a little more performance, consider using uwsgi or gunicorn. Both are good WSGI app servers that can be installed with pip along with your application.
gunicorn -w $(nproc) --threads 2 --max-requests 10 myproject:app