Threaded=true Flask larger application file structure - python

I recently created a web app and used the suggested file structuring from flask for separating views in different files. see here, http://flask.pocoo.org/docs/0.12/patterns/packages/.
But I also need to have the threaded parameter set to true which I would usually do at app.run(threaded=True) stage of testing. But I now run the app a different way, i tried putting "threaded=True" into my setup.py and that didn't do anything and was wondering how to get this working.

Just tell the flask run command to use threads:
$ export FLASK_APP=yourapplication
$ flask run --with-threads
Don't put this decision in your code; it is up to your WSGI server to run with threads or not.
You can see what options flask run accepts with flask run --help.

Related

Is it possible to have a Heroku app that is just a python console?

I have been writing a pretty simple python quizz system (called game.py) and I am working to deploy it on heroku. The app functions exclusively within the confines of a python console, with no interface of any kind but that provided by a terminal.
As such, I would like to be able to have the application on Heroku simply be akin to what you obtain with a one-off dyno, available on the dashboard (or in a terminal with the CLI) with:
heroku run python game.py
The application works perfectly well in it's deployed form (exclusively from the Heroku git) and locally, but in order for the app to be available to a larger public, I would need to have such a console appear on the "https://[appname].herokuapp.com/" URL that you are given on deployment of the app.
Naively, I would think this to be unspeakably simple to pull off, but I have yet to find a way to do it.
The only reasonable thing I have found would have been to create a Procfile, but lacking any documentation on the commands available, I only have been able to try variations of:
web: run python game.py
Which doesn't create a web console. And:
web: bash
Which simply crash with error code h10, with no other information given.
Any help, any suggestion, any workaround you can think of would be extremely appreciated.

What is the difference between using flask run vs python app.py vs python -m flask run? [duplicate]

This question already has answers here:
How to run a flask application?
(6 answers)
Closed 3 years ago.
The following ways allows me to launch the Flask server.
Option 1:
set FLASK_APP = app.py
flask run
Option 2:
set FLASK_APP = app.py
python -m flask run
Option 3:
python app.py
What is the difference between using either of this?
$ python app.py
This is the simplest, standard way of calling the Python interpreter to run any Python script. It is not specific to Flask. The app.py may or may not have a if __name__ == "__main__" block (see What does if __name__ == "__main__": do?), but if you are going to do this for Flask, it is required to have __main__ method that calls app.run().
From the Flask docs:
The alternative way to start the application is through the
Flask.run() method. This will immediately launch a local server
exactly the same way the flask script does.
Example:
if __name__ == '__main__':
app.run()
The docs also state why even though this works, it is not recommended:
This works well for the common case but it does not work well for
development which is why from Flask 0.11 onwards the flask method is
recommended. The reason for this is that due to how the reload
mechanism works there are some bizarre side-effects (like executing
certain code twice, sometimes crashing without message or dying when a
syntax or import error happens).
This way is also problematic if you need to modify run configurations (ex. port) depending on the host environment. For example, you need to use port 5500 instead of the default 5000 when running on a certain machine. You can of course do this with os.environ and app.run(port=5500), but it's going to be "messy" modifying the code based on environment-related configs that are unrelated to the code.
That is why we have the second way, the flask command line tool.
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello, World!'
$ set FLASK_APP=app.py
$ flask run --port=5500
You can now maintain your code to be independent of any external environment configurations. Aside from that, the flask CLI tool has a lot of other options for configuration and debugging, such as enabling/disabling DEBUG mode, listing routes (flask routes), and getting env vars from .env files.
Notice also that your app does not have to explicitly call app.run and __name__ now isn't going to be __main__. This is helpful for cases where your app is just part of a bigger package and/or it needs to be run from some other directory. See the Larger Applications section of the Flask docs.
Finally, we have the third way:
$ python -m flask run
This is another standard way of running Python scripts. It is also not specific to Flask. From the docs:
When called with -m module-name, the given module is located on the
Python module path and executed as a script.
This means flask will be searched from the invoked python module search path. This is particularly useful when your environment has multiple versions of Python and you want to make sure you are using the correct Python version and env with Flask. It can also be useful when you have multiple Flask installations for multiple projects. It explicitly sets which Python interpreter to use to call the flask CLI tool.
$ python3.8 -m flask --version
Python 3.8.10
Flask 1.1.2
Werkzeug 1.0.1
$ python3.8 -m flask run
$ python3.7 -m flask --version
Python 3.7.4
Flask 1.1.1
Werkzeug 0.16.0
$ python3.7 -m flask run
$ python -m flask --version
Python 2.7.16
Flask 1.0.3
Werkzeug 0.14.1
$ python -m flask run
flask run
This one looks for an executable (called flask) on your PATH, the first one executes with a parameter run, which will make the flask helper run the application by calling FLASK_APP.
python -m flask run
This one looks for an executable called python on your PATH, the first one executes receiving -m as argument, which is supposed to run a module (flask) and then pass the parameter run to it.
The key difference here, is that as it executes the first found executable on PATH, you can run a totally different Flask from the first one. You can also run a different python version's Flask.
python app.py
This calls the first python executable on PATH, and passes app.py as argument. It will make python run the app.py script, which may or may not have app.run() (This is what starts Flask).
If you don't have anything inside app.py, it won't call Flask's server.

Running python flask gunicorn app from shell script

I build my flask app, it requires external files through the runtime.
when I build the app, meaning I run > gunicorn app:app (the app startup file is called app.py but that didn't bother). It runs awesomely.
Now when I decided to make a sort of shell script to execute it (actually to make a couple of dependencies and environment checking but for the sake of simplifying, I created startup.sh in the same directory as app.py and it contains only the following instruction unquoted: "gunicorn app:app"), it just throws errors.
and this is the last one ...
Please help..
yep just as I guessed, it should be run within the script inside a virtualenv so that it will execute python 3 :)

How to change and reload python code in waitress without restarting the server?

I am using waitress to serve the web application content like.
waitress-serve --port=8000 myapp:application
While developing, as I change code, I continuously had to restart the waitress-serve to see my changes. Is there a standard way I can automate this?
I know this is an old question but I had a similar issue trying to enable hot reload functionality for my REST API using Falcon framework.
Waitress does not monitor file changes but you could use hupper on top of it. Pretty simple:
$ pip install hupper
$ hupper -m waitress --port=8000 myapp:application
It works on Windows too.
Based on the comment by #Dirk, I found the archive.org link to the snippet mentioned. I was able to get Waitress reloading by using Werkseug directly. Using Werkzeug's decorator run_with_reloader causes the app to restart whenever a Python file changes. (Werkzeug is used within Flask so it should be available).
Additionally, the app.debug = True line causes Flask to reload template files when they change. So you may want both considering your particular situation.
import werkzeug.serving
#werkzeug.serving.run_with_reloader
def run_server():
app.debug = True
waitress.serve(app, listen='127.0.0.1:4432')
if __name__ == '__main__':
run_server()
Once I had my server set up this way, it auto-reloaded the server whenever any file changed.

How to run a simple python script on heroku without flask/django?

I'm trying to run a simple hello world python program on my heroku server. I'm new to heroku.I was able to successfully deploy my script to heroku.
My python script and procfile are given below,
hi.py
print("hello world")
Procfile
web: python hi.py
I got "Hello world" as output when i ran heroku run web on my terminal.But when i try to run the app using heroku web url it shows the following error.
Application Error An error occurred in the application and your page
could not be served. Please try again in a few moments.
What did i do wrong here? I'm newbie to heroku & its concepts, please do bare.
There are three types of dyno configurations available on Heroku:
Web -- receives web traffic.
Worker -- keeps processing tasks/queues in the background.
One-off -- executed once. e.g.: backup.
If you're interested in running a script, do not care about receiving web traffic on it, and don't have a queue to process, then One-off dynos are likely what you'll want to use. This would be useful for database migrations or backups and whatnot.
Minimal example below.
Sample one-off dyno with Heroku and python AKA “hello world”
This assumes you have already created your app on Heroku and are able to use Herolu CLI from the command-line.
A minimal “hello world” Python script would then look like this. Only 2 files required:
requirements.txt Required, but can be left empty.
task.py with content print("hello world")
Then deploy to Heroku, e.g.:
git add .;
git commit -m "My first commit";
git push heroku master
After that, you'll be able to run your script with heroku run python task.py (and should see the long-awaited hello world in the output.)
If you want to run your program at specific times, use the free Heroku Scheduler add-on.
FYI, Procfile is optional. If you set it to hello: python task.py then you'll be able to run your program with just heroku run hello.
(Note that leaving requirements.txt empty will trigger You must give at least one requirement to install (see "pip help install") warnings on deploy. It's just a warning though and doesn't prevent proper deployment of the program.)
I disagree and state you want flask
main_app.py
import flask
app = flask.Flask(__name__)
#app.route("/")
def index():
#do whatevr here...
return "Hello Heruko"
then change your procfile to web: gunicorn main_app:app --log-file -

Categories

Resources