1

How can I run the multiple instance of the flask application in same web-server and same port?

Currently my flask application has following directory structure:

├── app
│   ├── __pycache__
│   ├── static
│   └── templates
├── instance
├── src
    └── __pycache__

When application is begun using flask run, it runs on default port localhost:5000.

I am able to create a copy of the application and run on two ports 5000 & 5001, and it works fine.

However, I wanted to use only one server with an index.html file, where localhost:5000/index.html has 2 links for redirection to localhost:5000/app1/ for app1 and localhost:5000/app2/ for app2

This is to avoid using of extra port.

I have currently cloned app to app2, however don't know to run two apps.(Below is the directory structure)

├── app
│   ├── __pycache__
│   ├── static
│   └── templates
├── app2
│   ├── __pycache__
│   ├── static
│   └── templates
├── instance
├── src
    └── __pycache__

I have also checked at using Blueprints, but to my understanding they are meant to be used for different views.

3 Answers 3

3

You can use werkzeug middleware to do this. It allows you to mount several apps on mount paths like /app1/ and /app2/.

You can read more about it in werkzeug documentation here: https://werkzeug.palletsprojects.com/en/0.14.x/middlewares/

Sign up to request clarification or add additional context in comments.

1 Comment

Well, I could not solve it yet, but I feel this is the nearest solution. So, working on it.
0

You don't need to use two apps. You can have index.html include the following lines of code

@app.route('/app1')
def hello():
    return render_template('app1.html')

@app.route('/app2')
def hello():
    return render_template('app2.html')

This means you can visit app1 or app2 as specificed (localhost:5000/app1) without creating multiple apps

2 Comments

The idea here is not to have two different HTMLs. But to create two instances of the apps.
@AshwinGeetD'Sa you cannot create multiple instances of an app on the same port
0

I would say that you can ran 2 apps on different ports, and use a reverse proxy to do load balancing on them. Check out this guide.

As an alternative, you could run multiple instances of your app with something like gunicorn.

1 Comment

But we cannot run multiple instances of the apps in single port using gunicorn.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.