avatarEivind Kjosbakken

Summary

The web content provides a guide on how to run multiple Python Flask applications simultaneously using the DispatcherMiddleware from the Werkzeug library.

Abstract

The article addresses a common challenge faced by developers using Flask, a Python web framework, where multiple applications need to be run concurrently. It explains the difficulty of executing several Flask apps with their own main.py files and demonstrates a solution using the DispatcherMiddleware from Werkzeug. The author outlines the steps to import and alias multiple Flask apps, set up a run.py file to combine them, and modify their endpoints to avoid conflicts. The solution allows for modular code and simultaneous operation of Flask applications on a single server, with each app accessible through a unique URL prefix. The article also includes practical code snippets and a file structure example to guide developers through the process.

Opinions

  • The author believes that running multiple Flask applications at once is beneficial for code modularity.
  • They acknowledge the potential for import issues when dealing with multiple main.py files and provide a method to resolve these issues through aliasing.
  • The author suggests that using the DispatcherMiddleware from Werkzeug is an effective way to combine Flask applications into a larger, dispatchable one.
  • They point out a downside of the method: the necessity to modify the endpoints of all but one application to include a prefix (e.g., /1, /2).
  • The author emphasizes the importance of having a clear file structure to manage multiple Flask applications.
  • They recommend using the run_simple function from Werkzeug for running the combined Flask applications, with debugging and auto-reloading features enabled.
  • The article concludes with an optimistic view that following the provided instructions should enable developers to successfully run and access multiple Flask applications concurrently.

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 etc

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

The file structure of the application. Note that this is not all the files, you still need other files in the lib_management and book_information folders

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_2

Since 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 server

And 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/endpoint1

The 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/endpoint1

Note, 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:

Web Development
Flask
Python
Werkzeug
Recommended from ReadMedium