Python app on Azure Web Apps frequently restarts

If You are here, You are probably running python app on Microsoft Azure PaaS / Microsoft Azure Web Apps and You noticed a problem with frequent restarts of the application. A consequence of this may be a very long response time for some users (even more than few seconds).

It's all because of fact, that Python apps on Web Apps runs with FastCGI on IIS. The first thing is that in Web App settings You must turn "Always ON" option on. It will kepp IIS turned on even during user inactivity.
Next, You need tu keep FastCGI up and running. To do this, You need to create webjob that will do some request to the site frequently. It's important that this frequence should be less than 300s.
You can use Azure (Python) WebJob with requests package and remember that if You have multi-tenant app You must push request to every tenant (use IIS environment vars to do that - You can find them in Kudu console).

import time
import requests
import os

def ping():
    instance = os.getenv('WEBSITE_INSTANCE_ID', 0)
    url = 'http://yourapp.azurewebsites.net/'
    cookies = dict(ARRAffinity=instance)
    r = requests.get(url, cookies=cookies)
    print 'OK'

while True:
    ping()
    time.sleep(180)

Example WebJob: download

The last thing and the most important is to reconfigure IIS and FastCGI on IIS. One of the default FastCGI settings on IIS (and Azure Web Apps) is instanceMaxRequests - it indicates how many requests should FastCGI handle before respawning the process. By default it is... 300 (max value for instanceMaxRequests on Microsoft Azure Web Apps is 10000).
The second thing is the application pool recycling and periodic pool restart (by default both are about 300s in IIS on Azure). You also need to reconfigure it but remember that pool recycling and frequent restarts are sometimes good for Your application - You can configure it to take place at night when the traffic is lower (or other time when You have less users).
You can reconfigure FastCGI on IIS by creating applicationHost.xdt file in site directory (You can find it in app home directory). You can do it with FTP or Kudu console. This file is transforming Web App configuration.

<?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <system.applicationHost>
        <applicationPools>
            <add name="%XDT_SITENAME%" xdt:Transform="Insert" autoStart="true" managedRuntimeVersion="v4.0" startMode="AlwaysRunning">
                <processModel idleTimeout="00:00:00" />
                <recycling>
                    <periodicRestart time="00:00:00" />
                </recycling>
            </add>
        </applicationPools>
    </system.applicationHost>
    <system.webServer>
        <fastCgi>
            <application xdt:Locator="Match(fullPath)" xdt:Transform="SetAttributes(maxInstances,instanceMaxRequests)" fullPath="D:\Python27\python.exe" instanceMaxRequests="10000" />
        </fastCgi>
    </system.webServer>
</configuration>

Example applicationHost.xdt file: download

Of course You need to change configuration for proper Python version. In example above it is 2.7.

comments powered by Disqus