1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] How to process Azure messages from a trigger function synchronously?

Discussão em 'Python' iniciado por Stack, Setembro 11, 2024.

  1. Stack

    Stack Membro Participativo

    Azure proposes different trigger functions: for blob file, http request, service bus, etc ... The site Azure Functions HTTP trigger explains how to use them.

    These functions are asynchronous, aren't they? At least that's what we think, based on our experiment.
    The trigger function for service bus messages is defined as followed:

    def test_function(msg: func.ServiceBusMessage):
    message = msg.get_body().decode('utf-8')
    do_sthg(message)

    # Our function
    def do_sthg(var1):
    # The following won't be printed unly 10000 before the next incomming message, which is understandable, if the trigger function is async.
    for i in range(10000):
    print(var1)
    print(f"message: {var1}")


    How can we force the function to wait for the do_sthg() function to finish executing its code?
    Some operations need to be performed before the next incoming message.
    Is there a better way to do this?

    {
    "version": "2.0",
    "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[4.0.0, 5.0.0)"
    }
    "extensions": {
    "serviceBus": {
    "clientRetryOptions":{
    "mode": "exponential",
    "tryTimeout": "00:01:00",
    "delay": "00:00:00.80",
    "maxDelay": "00:01:00",
    "maxRetries": 3
    },
    "autoCompleteMessages": true,
    "maxAutoLockRenewalDuration": "00:05:00",
    "maxConcurrentCalls": 16,
    "maxConcurrentSessions": 8,
    "maxMessageBatchSize": 1000,
    "maxBatchWaitTime": "00:00:30",
    "sessionIdleTimeout": "00:01:00",
    "enableCrossEntityTransactions": false
    }
    }
    }


    I have played around with different parameter values, but I have not managed to find a way for my program to run message by message, or for the trigger function to wait for the do_sthg() function to finish.

    Continue reading...

Compartilhe esta Página