This question already has answers here:
Why does running the Flask dev server run itself twice?
(7 answers)
Closed 8 years ago.
As far as I understood Flask should create a thread and a second thread to run on it, but what I see is there are always two processes, not threads, running.
Even for the simplest app.
from flask import Flask
from flask import render_template, request, flash, session, redirect
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World!'
app.run(host="192.168.21.73", port=5000, debug=True)
You can see two process running:
ps -x
5026 ttyO0 S+ 0:01 /usr/bin/python ./test_flask.py
5031 ttyO0 Sl+ 0:45 /usr/bin/python ./test_flask.py
What is happening here?
It's because you're running the dev server with the reloader. The reloader monitors the filesystem for changes and starts the real app in a different process, so there are two total processes.
You can disable the reloader by settting debug=False or use_reloader=False when calling run.
Related
This question already has answers here:
Configure Flask dev server to be visible across the network
(17 answers)
Closed 3 months ago.
The Flask server is up and running on my laptop (Ubuntu), with debugger ON:
(venv) deeman#carbon:~/flask_dir/venv/dox$ flask --app hello_w run
* Serving Flask app 'hello_w'
* Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:5000
Press CTRL+C to quit
* Restarting with stat
* Debugger is active!
* Debugger PIN: 130-661-482
Bellow is the basic server page in python
app = Flask(__name__)
#app.route("/")
#app.route("/hello")
def hello_world():
return "<p>Hello, World!</p>"
#app.route("/test")
def hello_world2():
return "<p>Test works!</p>"
if __name__ == "__main__":
app.debug = True
app.run(host="0.0.0.0", port = 5000)
On any other devices connected to the same Wifi network I get Can't connect to Server or Site can't be reached
What am I missing? Can someone help me understand issue? Thanks
Thank you all. #DaveW.Smith provide the solution in my case, which was:
to simply pass --host=0.0.0.0 as an argument, as bellow:
$ flask --app hello_w run --host=0.0.0.0
This question already has answers here:
Deploying a minimal flask app in docker - server connection issues
(8 answers)
Are a WSGI server and HTTP server required to serve a Flask app?
(3 answers)
Closed 2 years ago.
I have a flask app, with Flask RestX, that works fine with flask run but has import issues with Docker. I've seen some questions and answers about this but they all involve Gunicorn, but I'm not using Gunicorn.
Here is my folder structure. I'm running the docker-compose file which uses the flask.dev.dockerfile as well as a Redis container.
So, I want to use my redis wrapper, RedisDatabase, from my database.py file. PyCharm inserts this as
from ivd_app.database import RedisDatabase
And the rest of the __init__.py file, in summary:
from flask import Flask, request
from flask_cors import CORS
from flask_restx import Api, Resource, fields
def create_app(db=None):
app = Flask(__name__, instance_relative_config=True)
if (db is None):
db = RedisDatabase()
api = Api(app, version='0.0.1', title='IVD')
config_endpoint = api.namespace(
'config', description='APIs to Send configuration to and from the Front End'
)
config_model = api.model('Configuration', {
# the model
})
#config_endpoint.route('/')
class ConfigurationEndpoint(Resource):
#config_endpoint.expect(config_model, validate=True)
def put(self):
# the put endpoint
def get(self):
# the get endpoint
return app
# if __name__ == "__main__":
# create_app().run(host="0.0.0.0", debug=True)
When I use CMD flask run from the dockerfile after setting the environment variables, the application runs. But for some reason I can't access the flask server, so instead I uncomment the lines at the bottom, since I know those work, and use CMD python ivd_app/__init__.py in the Dockerfile. But the container exits:
flask_1 | Traceback (most recent call last):
flask_1 | File "ivd_app/__init__.py", line 38, in <module>
flask_1 | from ivd_app.database import RedisDatabase
flask_1 | ModuleNotFoundError: No module named 'ivd_app'
If I remove the import statement and put the RedisDatabase class in the __init__.py file, the entire app works, of course. But I want to separate it into a different file.
How do I fix this?
Flask by defaults binds to 127.0.0.1 so if you use CMD flask run it'll reject connections from outside the container.
If I remember correctly you must either do
ENV FLASK_RUN_HOST 0.0.0.0
CMD flask run
or
CMD flask run --host 0.0.0.0
As for the other method, I'd try
CMD python -m ivd_app
since you appear to try running a module rather than a standalone script.
This question already has answers here:
How can I change the host and port that the flask command uses?
(6 answers)
Closed 6 years ago.
I am trying to access my flask program on localhost from other devices like Android phone in my network.
I can access my localhost created using Apache but can not access program created by Flask.
This is my Flask program:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def home_page():
return '<h1>Welcome to my site!</h1>'
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5050, debug=False)
When I run it, it always listens 127.0.0.1:5000.
* Serving Flask app "server"
* Forcing debug mode off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
I can see my PHP files in localhost but cannot see Flask program. (i.e. http://my_ip:5000 doesn't work)
Simply change '0.0.0.0' to your LAN IP address. You can know that typing ifconfig on Linux or ipconfig in windows in a cmd or a terminal
For Example
app.run(host="192.168.1.41")
When I try to update the __init__.py file in Flask, it doesn't show the changes in the server, but when I edit home.html it works fine.
app/__init__.py
import os
from flask import Flask, render_template
from werkzeug.contrib.fixers import ProxyFix
app = Flask(__name__)
#app.route('/')
def home():
return render_template('home.html')
app.wsgi_app = ProxyFix(app.wsgi_app)
app.debug = bool(os.environ.get('PRODUCTION'))
if __name__ == '__main__':
app.run()
Any tips?
We solved the problem in comments but I will add solution here if someone else has a similar problem.
For development environment add debug=True argument to your app
app.run(debug=True)
If your development environment works on an application server, then you should look for autoreload option. In uWSGI there is py-auto-reload for example.
For released, stable environment you should restart your application server.
For example in uWSGI
There are several ways to make uWSGI gracefully restart.
# using kill to send the signal
kill -HUP `cat /tmp/project-master.pid`
# or the convenience option --reload
uwsgi --reload /tmp/project-master.pid
# or if uwsgi was started with touch-reload=/tmp/somefile
touch /tmp/somefile
More: http://uwsgi-docs.readthedocs.io/en/latest/Management.html#reloading-the-server
Warning: if you combine application and web server, uWSGI and Nginx for example, then restarting Nginx won't reload your application code. Focus on the application server.
I have web application written in Flask. As suggested by everyone, I can't use Flask in production. So I thought of Gunicorn with Flask.
In Flask application I am loading some Machine Learning models. These are of size 8GB collectively. Concurrency of my web application can go upto 1000 requests. And the RAM of machine is 15GB.
So what is the best way to run this application?
You can start your app with multiple workers or async workers with Gunicorn.
Flask server.py
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run()
Gunicorn with gevent async worker
gunicorn server:app -k gevent --worker-connections 1000
Gunicorn 1 worker 12 threads:
gunicorn server:app -w 1 --threads 12
Gunicorn with 4 workers (multiprocessing):
gunicorn server:app -w 4
More information on Flask concurrency in this post: How many concurrent requests does a single Flask process receive?.
The best thing to do is to use pre-fork mode (preload_app=True). This will initialize your code in a "master" process and then simply fork off worker processes to handle requests. If you are running on linux and assuming your model is read-only, the OS is smart enough to reuse the physical memory amongst all the processes.