Improve Flask Performance on MS IIS

Mustafa Abusalah
3 min readOct 14, 2021

This article will demonstrate how to reduce response time when calling Python using Flask framework on Microsoft Internet Information Services 8.

Flask is a microframework for web development using Python

I came across this challenge after developing an NLP tool using Python 3.9 to find similarities of new articles before posting them by comparing them to existing articles in the repository. After building the tool I had to install Flask on IIS, this is explained in this article and this one.

Once I successfully deployed my app, and when calling the API it took about 20 seconds to respond, this sounded odd especially that I tested the code on Jupyter notebook and the response time was incredible. I decided to make an investigation which started by consulting a friend saddam abu ghaida who advised me to increase the idle timeout to the maximum which is 2592000 seconds this is translation to 12 hours. By increasing the idle timeout you tell the system to terminate the python process if it does not communicate with the IIS worker process within 12 hours, which means the process will not be terminated. To set the Idle Timeout:

Click on your IIS root, then click on FastCGI Setting
Right Click on the path and then click on Edit
Now set the Idle Timeout to 2592000, this is the maximum number you can have

This was of great help but after making the first request to the server. In the past each request used to take over 10 seconds and some times up to 30 seconds, now except from the first request everything works fast. But this is not enough I want the first request to be fast. Further to my friend notice, I had to make further investigation which concluded that flask has to stay always running on IIS, to do so I followed the below steps:

Now click on Application Pool, select flask or what ever you named your application and click on Advanced Settings

Under General set the Start Mode to AlwaysRunning and under Process Model the Idle Time-out (minutes) to 0.

Now the below step tells IIS if the service or server was restarted to load Flask immediately without waiting for the first call:

After you finished the previous step, go to your flask site right click and then click on Manage Website and then advanced Settings
Set the Preload Enabled to True. This option tells the IIS to Preload Flask once it is restarted or started otherwise it will load on first request which will cause a delay.

Those steps have contributed positively and improved the response time of my flask application. Those steps should work well with other frameworks like Django.

Good Luck!

--

--

Mustafa Abusalah

Manager of Learning & Innovation. Expert in Information Retrieval, Machine Learning, NLP & KM.