'Making simultaneous calls with images to FASTapi using request-async library in python not working

Following is the sample code making 10 calls to the API.

URL = "http://127.0.0.1:3000/v1/img_process"

CALLS = 10

payload = {"data": "value1, value2, value3, value4, value5, value6"}
files = {'file1': open('sample1.jpg', 'rb'),
        'file2': open('sample1.jpg', 'rb')}


headers = {
    "Authorization" : "Bearer XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
}

async def request_post():
    return await requests.post(url=URL,headers=headers, data=payload, files=files) 

async def main():
    start = datetime.now()
    tasks = [asyncio.create_task(request_post()) for _ in range(CALLS)]
    result = await asyncio.gather(*tasks)

    end = datetime.now()

    print("Start time = " + start.strftime("%H:%M:%S") + "\n" + 
        "End time = " + end.strftime("%H:%M:%S") + "\n" +
        "Elapsed = " + str(end - start) + "\n" + 
        "Seconds per call = " + str((end - start)/CALLS)
        )


await main()

Out of the 10 calls only the 10th call(last) is being executed by the API, in the first 9 cases the API is throwing the following error

PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x000002115FA50E00>

The code for image reading at the endpoint is

def read_file_as_image(byte_data) -> np.ndarray:
    image = np.array(Image.open(BytesIO(byte_data)))
    return image 

@app_v1.post("/img_process")
async def inputs(data: List[str] , file1: UploadFile = File(...), file2: UploadFile = File(...)):

img1 = read_file_as_image(await file1.read())
img2 = read_file_as_image(await file2.read())

Could this be the issue with using the the async/await function while reading the images Please suggest some solution



Solution 1:[1]

Have you looked into FatsAPI BackgroundTask?

https://fastapi.tiangolo.com/tutorial/background-tasks/

You can add the task read_file_as_image() in BackgroundTask as it takes some time to process image.

It also could be that the function read_file_as_image is not truly async and multiple calls cancel execution and last call is only executed fully.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Reinis Ga??is