Search code examples
pythonazure-functionsapache-synapsespark-notebook

is it possible to limit simultaneous requests towards azure function using threadspool in python?


I'm working on setting up the configuration to send arrays containing the body to the Azure function. However, even when using time.sleep, the requests to the Azure function continue, causing the function to crash. I'm doing this work in a Synapse notebook. The idea is to send these arrays, knowing that Azure Function accepts 200 requests, and then it should pause before continuing to work with the next 200 requests, and so on consecutively

def readarray(array):
    try:
        print(f"array: {array}, thread: {threading.currentThread().getName()}")  
        time.sleep(10)
        print(f"Array {array} processed")
    except Exception as e:
        print(f"Error array {array}: {str(e)}")    

try:
    array_count = 0
    with ThreadPoolExecutor(max_workers=5) as executor:
        # Use threadpool
        executor.map(readarray, array_of_objects)
except Exception as e:
    print("Error:", str(e))

Solution

  • you could split your data with a batch of 200, deal with it and don't deal with the next batch until previous finished.

    As no detailed function in your question, I assume you try to call a remote function ‘your_function_url’ with post method:

    import time
    import requests
    import concurrent.futures
    
    
    def read_array(data):
        response = requests.post('your_function_url', json=request_data)
        print(response.status_code)
    
    def send_requests_to_azure(batch):
        with concurrent.futures.ThreadPoolExecutor() as executor:
            executor.map(read_array, batch)
            concurrent.futures.wait(tasks)
    
    array_of_objects = [ ... ]  # Your array of data
    batch_size = 200
    
    for i in range(0, len(array_of_objects), batch_size):
        current_batch = array_of_objects[i:i+batch_size]
        
        send_requests_to_azure(current_batch)