Run multiple Python Flask applications at the same time
Flask is an easy-to-use web framework with Python. Sometimes when creating a web application with Flask, you might create several applications, that is, multiple main.py files where you create your endpoints. This can be good as it makes the code more modular. The problem with this however is that you cannot just run the different flask applications all at once. In this article, I will show you exactly how to achieve this using the DispatcherMiddleware from the Werkzeug library.
Run two Flask applications
First, you need at least two Flask applications, which I assume you already have in this case.
You then have to import the applications from the main.py in the two applications. This is where I first encountered some issues when trying to run several Flask applications at the same time. I have two main.py files, and I create an app in both as you can see in the code below. It does not matter that they are both called “app”.
#main.py file number 1. File path: application/lib_management/main.py
app = Flask(__name__)
#... create endpoints etc#main.py file number 2. File path: application/book_information/main.py
app = Flask(__name__)
#... create endpoints etcNow I create a run.py file in a place where I want to run my Flask applications. I then import the applications from the two main.py files mentioned above. You can potentially encounter some issues here. The first one is how to import the main.py files, which I think can be a bit tricky in Python. In my case, I first store my main.py files in folders called “lib_management” and “book_information”. I then store my run.py file on the same level as the two folders. You can see the file structure in the image below:

Then you can import the applications like this:
#run.py file
#import apps from the two main.py files
from lib_management.main import app as flask_app_1
from book_information.main import app as flask_app_2Since the applications are called “app” in both files, it is important to import the app as an alias, so in my case, I call the apps “flask_app_1” and “flask_app_2”.
You also need some imports in the run.py file. First, install the Werkzeug package:
pip install Werkzeug
Then import the required imports:
#in run.py file
from werkzeug.middleware.dispatcher import DispatcherMiddleware # use to combine each Flask app into a larger one that is dispatched based on prefix
from werkzeug.serving import run_simple # werkzeug development serverAnd then finally you can run your app. There is one downside however, and that is that you can have one application run on its normal endpoint, but the others will have to have a slightly modified endpoint (in my example I just add a “/1” to the endpoint, but you can add whatever you prefer. The important thing is that if you do not modify the endpoint, the first application will not work.
#in run.py
#run your application
if __name__ == "__main__":
application = DispatcherMiddleware(flask_app_1, {'/1': flask_app_2})
run_simple('localhost', 5000, application, use_reloader=True, use_debugger=True, use_evalex=True)
Your application will now be accessible on the 5000 port. Naturally, you can extend this to have a third app, by just changing the code like below:
#import the third flask app
from user_management.main import app as flask_app_3
#run your application with 3 applications
if __name__ == "__main__":
application = DispatcherMiddleware(flask_app_1, {'/1': flask_app_2, '/2': flask_app_3})
run_simple('localhost', 5000, application, use_reloader=True, use_debugger=True, use_evalex=True)To start the application you write “python run.py” in the folder of the run.py file
Access endpoints
Now your multiple application Flask app is hopefully running, and you want to access your endpoints. Let's pretend we had 3 endpoints like below if we were to run each application separately.
#lib_management endpoint (flask_app_1)
http://localhost:5000/lib_management/endpoint1
#book_information endpoint (flask_app_2)
http://localhost:5000/book_information/endpoint1
#user_management endpoint (flask_app_3)
http://localhost:5000/user_management/endpoint1The new endpoints for these would now be:
#lib_management endpoint (flask_app_1) -> url does not change!
http://localhost:5000/lib_management/endpoint1
#book_information endpoint (flask_app_2) -> add /1/
http://localhost:5000/1/book_information/endpoint1
#user_management endpoint (flask_app_3) -> add /2/
http://localhost:5000/2/user_management/endpoint1Note, the /1/ or /2/ are added right after the localhost:5000, since they are just an addition to the port they are running on.
And that's it, now you should hopefully be able to run and access multiple Flask applications at the same time. If you have any questions, just ask them in the comments!
If you want to check some other related articles I have written, please check out:





