asyncio within Azure Functions

Benjamin Richey Halladay 20 Reputation points
2025-03-18T01:42:06.6366667+00:00

I have an idea that I want to run by someone with a bit more experience (I'm new to Azure Functions). I want to create a centralized object that processes multiple requests (from a service bus in this case) at the same time. I need to be able to group different requests into the same group for certain API calls.

So, what I've done is created a class that can't be created twice, like so:

import asyncio
import logging

class MasterClass:
    _instance = None

    # --- By overriding the __new__ method, we can ensure that only one instance of the MasterClass is ever created for all recieved requests
    def __new__(cls):
        if not cls._instance:
            cls._instance = super(MasterClass, cls).__new__(cls)
        return cls._instance
    
    def __init__(self):
        if not hasattr(self, 'init'):
            self.count = -1
            asyncio.create_task(self.exit())
            self.init = True

    async def add(self, params):
        self.count = 1 if self.count == -1 else self.count + 1
        logging.info(f"initiating request with params: {params}, {self.count} in line")
        await self.proccess_request()
        self.count -= 1

    async def proccess_request(self):
        await asyncio.sleep(200)
        return

    async def exit(self):
        while self.count != 0:
            await asyncio.sleep(2)
        # spot to put the needed awaits for certain indefinite taks
        logging.info(f"shutting down")
        return

Then, my function_app.py looks like this:

import azure.functions as func
import logging
import asyncio

from master_class import MasterClass

app = func.FunctionApp()

@app.service_bus_queue_trigger(arg_name="azservicebus", queue_name="copycat-queue",
                               connection="fscadmgmtsbcb5ee72a_SERVICEBUS") 
async def test_sb_func(azservicebus: func.ServiceBusMessage):
    logging.info('Python ServiceBus Queue trigger processed a message: %s',
                azservicebus.get_body().decode('utf-8'))
    
    master_class = MasterClass()
    await master_class.add(azservicebus.get_body().decode('utf-8'))

It seems to work locally, but I'm not sure I'll get any adverse effects in Azure Functions. If I set FUNCTIONS_WORKER_PROCESS_COUNT to 1, will it only ever create the MasterClass once? I understand once all requests are done it'll be destroyed, which is fine, I just want it to only ever have once instance of the class while running.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,909 questions
{count} votes

Accepted answer
  1. Sai Prabhu Naveen Parimi 2,260 Reputation points Microsoft External Staff Moderator
    2025-03-18T03:50:58.47+00:00

    Benjamin Richey Halladay

    Yes, setting FUNCTIONS_WORKER_PROCESS_COUNT = 1 ensures a single instance of MasterClass per worker process, but not across multiple scaled-out instances of your function app. If scaling is a concern, consider Durable Functions or an external state management approach.

    Recommended Approach

    Ensure a Single Instance Per Worker Process:

    Handle Scale-Out with Durable Functions:

    • Use Durable Functions Singleton Pattern to ensure a single orchestrator runs per function app.
    • Assign a fixed Instance ID to the orchestrator to prevent multiple instances from executing at the same time.

    Group Requests with Service Bus Sessions:

    • If messages need to be processed in specific groups, enabling Service Bus Sessions ensures that all messages with the same Session ID go to the same function instance.

    This approach balances scalability, state consistency, and efficient message grouping for high-performance processing in Azure Functions.

    Let me know if you need further clarification!

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.