21

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

JKC
  • 2,498
  • 6
  • 30
  • 56
  • 2
    It shouldn't work as both will run in the same port and the second one should complain. You have to add a `port` argument to the `app.run()` call using two distinct ones. – Ignacio Vergara Kausel Jan 11 '18 at 11:10
  • Thanks @IgnacioVergaraKausel but on a single day my script needs to be executed with 'n' number of arguments. As a standalone python script it is working fine. But any idea on how to achieve the same when we need to expose the python script as a web service ? – JKC Jan 11 '18 at 11:11
  • It's not clear to me what are you further asking, which is not part of your original question. – Ignacio Vergara Kausel Jan 11 '18 at 11:19
  • sorry @IgnacioVergaraKausel My original question is just a simplified question for better understanding. But if you see the title of the post you can understand my real requirement – JKC Jan 11 '18 at 11:28
  • You could start the two applications on diffrent ports (just add `port = 5001` to the `app.run` call to start the app at port 5001 instead of 5000) – Sebastian Stigler Jan 11 '18 at 12:07

3 Answers3

34

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
metmirr
  • 4,234
  • 2
  • 21
  • 34
  • 4
    Is there any problem with having multiple instances of flask open on different ports like this? As long as my computer can handle it, there aren't any other issues? – rvictordelta Jun 25 '19 at 14:48
  • @rvictordelta and anyone else that may be wondering. This is a normal way of separating two flask applications. You specify a port that is not running another application, as only one per port should be used. This will cause no issues as long as you don't overload your computer with servers. – Chase Quinn Feb 11 '22 at 19:42
2

Although other answer suggests to run both flask servers with different --port arguments, I wanted to share another issue to consider:

Clear the browser cache

Did you clear cache memory before running the second script?

There are times when browser has previous data of the URL (including first port) still stored. So it will display old data (from first script and port) instead of fetching new data (from second script and port).

You can clear the cache memory and then run the second script, access in browser and see it fetches the data correctly from the second app's URL.

hc_dev
  • 8,389
  • 1
  • 26
  • 38
blank
  • 21
  • 1
  • thank you for your inputs. but already this is fixed and an answer has been accepted – JKC Sep 17 '19 at 02:22
2

I have very simple solution for this problem just add this below line in your code app.run(port=3000)

simply assume that your first application is working using 5000 port and you try to execute another flask app it may using the same port which is 5000 so that's why you may face these problem so just simply add this line of code in which you can change your port number to execute the application on another port.

example:

if __name__ == '__main__':
    app.run(port=3000)

Note: instead of port 3000 you can enter any of your choice

a.mola
  • 3,883
  • 7
  • 23
  • Your answer explains what [comment of Ignacio](https://stackoverflow.com/questions/48205495/python-how-to-run-multiple-flask-apps-from-same-client-machine#comment83392291_48205495) suggested. However OP asked for __multiple__ server-applications. Could you give an example, how to fix both given scripts - see how [metmirr illustrated both `script1` and `script2`](https://stackoverflow.com/a/48208710/5730279). – hc_dev Nov 18 '21 at 23:38