Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - How to run multiple flask apps from same client machine

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

like image 301
JKC Avatar asked Jan 11 '18 11:01

JKC


People also ask

How to run multiple flask apps on one machine?

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:

What is flask-apscheduler in Python 3 flask?

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.

How many clients can flask run at once?

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.

How to enable multiple threads in flask?

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 ):


1 Answers

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
like image 107
metmirr Avatar answered Sep 29 '22 22:09

metmirr