Search code examples
pythonpython-3.xazureservicebusazure-web-app-servicepolling

How to execute a long running python service bus polling job in Azure App Service environment


I am quite new to Azure cloud and working on a python backend application deployment in Azure python 3.8 app service. It is a standalone app that has a infinite loop to poll messages from Azure service bus queue and does some operation accordingly. Note that this app does not run on any of the common web framework like Flask and Django.

The issues is, the app service is continuously restarting post deployment to the azure cloud. The app has application.py file already present in the root folder.

Here is the application.py code structure:

import os
import time
from flask import Flask
app = Flask(__name__)


def poll_asb_queue():
while True:
    print('I am still polling’)
    # Poll queue logic here
    # Do logic based on the message payload
    time.sleep(5)

### Flow starts here ###
try:
    poll_asb_queue()

except Exception as error:
print('ERROR', str(error))

Replacing this polling logic with a flask based deployment having a hello world endpoint seems to work fine.

Please help me understand where am I going wrong, and how I can run a long running standalone python application in azure app service


Solution

  • If you're wanting to monitor messages on a service bus, you don't need a long polling web job. You can go with an Azure Function that uses an http trigger to invoke when a new message arrives. See https://learn.microsoft.com/en-us/azure/azure-functions/functions-create-first-azure-function-azure-cli?pivots=programming-language-python&tabs=cmd%2Cbrowser#deploy-the-function-app-project-to-azure.

    This will be more efficient because you can forego the AlwaysOn App Service necessary for your webjob being set to Continous, which is probaby what's causing your issue.