ARTICLE AD BOX
I have a mostly CPU-bound task that contains a subtask calling an API (specifically AWS Bedrock through boto3).
I would like to parallelise running this task for several inputs at a time:
I have the computational resources to run more than one CPU task at a time.
AWS Bedrock has a quota that I should leverage.
I currently have this set-up:
import asyncio semaphore = asyncio.Semaphore(5) def main(): tasks = [_process_with_limit(url) for url in urls] return await asyncio.gather(*tasks, return_exceptions=True) async def _process_with_limit(url: str): async with semaphore: return await async_wrapper_mostly_cpu_bound_function(url) async def async_wrapper_mostly_cpu_bound_function(url_to_pdf: str): return await asyncio.to_thread(mostly_cpu_bound_function, url_to_pdf=url_to_pdf) def mostly_cpu_bound_function(url_to_pdf: str): # Local PDF to Markdown conversion (no API calls, all locally) extracted_images, markdown_content = cpu_based_conversion(url_to_pdf) # API calls to AWS Bedrock image_captions = asyncio.run(bedrock_api_calls(extracted_images)) return markdown_content, extracted_images, image_captions async def bedrock_api_calls(extracted_images: list[str]) -> list[str]: tasks = [ caption_image(image) for image in images.items() ] return await asyncio.gather(*tasks) def caption_image(image: str): # Essentially boto3.client().converse() pass def cpu_based_conversion(url_to_pdf: str): # Essentially docling powered conversion of PDF bytes passA few considerations:
I understand that boto3 is not async by nature
The function main essentially enables a FastAPI endpoint
Is the use of async beneficial in this context and if so, am I using it correctly?
If it is not beneficial, which set-up would you recommend to parallelise tasks (AWS powered solutions are welcome)?
