代码之家  ›  专栏  ›  技术社区  ›  tavo92

在python中使用threadspool可以限制对azure函数的同时请求吗?

  •  0
  • tavo92  · 技术社区  · 1 年前

    我正在设置配置,将包含正文的数组发送到Azure函数。但是,即使使用time.sleep,对Azure函数的请求也会继续,导致函数崩溃。我在Synapse笔记本上做这项工作。这个想法是在知道Azure Function接受200个请求的情况下发送这些数组,然后在继续处理接下来的200个请求之前暂停,依此类推

    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))
    
    0 回复  |  直到 1 年前
        1
  •  0
  •   linpingta    1 年前

    你可以将数据分成200个批次,处理它,在前一个批次完成之前不要处理下一个批次。

    由于您的问题中没有详细的函数,我假设您尝试使用post方法调用远程函数your_function_url:

    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)