If you have followed my previous post for hosting a Django or flask app on IIS then this post is for some additional settings to improve the performance of our app in IIS. I recommend following that post to see how we can host a Django or flask application on IIS.
To read more about Django go to official page of Django Project.
Django runs under FastCGI in IIS. With the following processes.
- w3wp.exe – It is the IIS worker process for the application pool.
- python.exe – The main FastCGI process for the Django or flask applications.
With default settings the application pool recycles every fixed time interval. And the system terminates the python process if it does not communicate with the IIS worker process with in that time interval.
Also the server does not create the application pool until an HTTP request comes in for the first time.
Due to this if no request comes in within that time interval, The system will completely terminate our app process so every resource loaded by that process also will be recycled and whenever a new request comes in after that, IIS needs to recreate the pool and so IIS also needs to recreate the python process. It will only be able to serve response after creating the processes and loading the resources again. In this scenario, the server needs some time to set up our app and we see performance weakness for our app in IIS and also late response.
While using heavy machine learning models which take time to load, This type of setup is useless to predict instantly from our model if the pool is recycled or if it is a first request to the IIS server.
Improve performance of Django and flask on IIS
Step 1 – Open IIS Manager with “inetmgr” command in Run menu.
Step 2 – Click on the server name and then on FastCGI Settings.
FastCGI Application Settings
Step 3 – On the next window select your application from the middle pane and then select “Edit” from Actions under the right pane.
Step 4 – Now in the “Edit FastCGI Application” dialog under “Process Model” edit the “Idle Timeout” and set it to 2592000 which is the max value for that field in seconds. See the description below while editing the field to know more about that field.
Step 5 – Click OK.
IIS Application pool settings
Step 6 – Then Expand the IIS server and go to Application Pools.
Step 7 – Right-click on the pool which is set for the Django or Flask application and select “Advanced Settings”.
Step 8 – After that in Advanced Settings in the General Section set Start Mode to AlwaysRunning. It will keep the process alive.
Additionally you can increase the “Maximum Worker Process” also but don’t set it too high. This will create multiple w3wp.exe processes for the application pool.
Step 9 – In Process Model Section set Idle Timeout to 0.
Step 10 – Finally in Recycling Section set Regular Time Interval to 0. This will prevent the pool to be recycled. You can set a specific time also on which application pool should be recycled. Use 0 to not recycle our app pool.
IIS Website Settings
Step 11 – (Optional) To preload the application on startup of IIS server, Expand sites > right-click on your site > Manage Website > Advanced Settings
Set “Preload Enabled” to True. If it is false then the site will load when the first request comes in.
So we have learned how we can improve performance of Django and flask on IIS.