pythonazureiisazure-web-app-servicewfastcgi

Disable replica services in Web Apps


I have written a service that reads from the file system in order to determine state, and holds that state for a while instead of constantly updating the file system. Therefore it's paramount that there is only one instance of that service. Now it seems Web Apps instantiates two instances of a Flask service by default.

Is this a documented thing, or have I understood some configuration wrong? Are they in fact replicas or some other mechanism? And most importantly, how can I disable this feature?

My application factory:

from flask import Flask


def create():
    app = Flask(__name__)
    d = {'t': 0}

    @app.route('/', methods=['GET'])
    def main():
        d['t'] += 1
        return 'Calls: %d' % d['t'], 200

    return app

And web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="PYTHONPATH" value="D:\home\site\wwwroot" />
    <add key="WSGI_HANDLER" value="app.create()" />
    <add key="WSGI_LOG" value="D:\home\site\wwwroot\wsgi.log" />
  </appSettings>
  <system.webServer>
    <handlers>
        <add name="PythonHandler" path="*" verb="*" modules="FastCgiModule"
        scriptProcessor="D:\home\python364x64\python.exe|D:\home\python364x64\wfastcgi.py"
        resourceType="Unspecified" requireAccess="Script"/>
    </handlers>
  </system.webServer>
</configuration>

After startup, these calls return a sequence like 1, 1, 2, 2, 3, 3... which to me indicates that there are two instances running and a balancer round-robining the two. The pattern seems to be fairly regular, not skipping the other service for example.


Solution

  • The answer was a simple one. Applications created under a service plan may be scaled to a number of instances by default. In my case it was indeed set to two. One can view the number of instances in the application view under Scale Out.

    Setting the number of instances back to one solved the issue. But in case other services under that plan require the number of instances to stay the same, creating a new service plan with no additional instances is required.