I have one flask application script as given below :
from flask import Flask
app = Flask(__name__)
@app.route("/<string:job_id>")
def main(job_id):
return "Welcome!. This is Flask Test Part 1"
if __name__ == "__main__":
job_id = 1234
app.run(host= '0.0.0.0')
I have another flask application script as below :
from flask import Flask
app = Flask(__name__)
@app.route("/<string:ID>")
def main(ID):
return "Welcome!. This is Flask Test Part 2"
if __name__ == "__main__":
ID = 5678
app.run(host= '0.0.0.0')
The only difference between both the scripts is the argument name and its value. Now my question is assume that I am executing the first script. So I will get something like
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
When I execute http://127.0.0.1:5000/1234 in my browser I am able to see
"Welcome!. This is Flask Test Part 1"
Now with this server active, I am executing the second script. So again I get
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
But when I execute http://127.0.0.1:5000/5678 in my browser I am able to see
"Welcome!. This is Flask Test Part 1"
instead of
"Welcome!. This is Flask Test Part 2"
I don't understand where I am doing mistake. Any inputs or alterations will be helpful
32 Flask development server by default listens on port 5000so when you run a Flask app without port number it will run on 5000. You can run a number of Flask app on the same machine but with the different port numbers. Let's say your scripts names are script1.pyand script2.py:
When you are building your HTTP server with Python 3 Flask, Flask-APScheduler gives you the facilities to schedule tasks to be executed in the background. In this post, we look at how we can get Flask-APScheduler in your Python 3 Flask application to run multiple tasks in parallel, from a single HTTP request.
Using the simple app.run () from within Flask creates a single synchronous server on a single thread capable of serving only one client at a time. It is intended for use in controlled environments with low demand (i.e. development, debugging) for exactly this reason.
From Flask 1.0, it defaults to enable multiple threads ( source ), you don't need to do anything, just upgrade it with: If you are using flask run instead of app.run () with older versions, you can control the threaded behavior with a command option ( --with-threads/--without-threads ):
Flask development server by default listens on port 5000
so when you run a Flask app without port number it will run on 5000
.
You can run a number of Flask app on the same machine but with the different port numbers. Let's say your scripts names are script1.py
and script2.py
:
$ export FLASK_APP=script1.py
$ flask run --host 0.0.0.0 --port 5000
Open up a new terminal
$ export FLASK_APP=script2.py
$ flask run --host 0.0.0.0 --port 5001
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With