'Unable to achieve async/parallel behavior inside for loop while using FastAPI and Ormar

I am using FastAPI and have an async route that needs to do many things like making calls to other API endpoints, and reading/writing to a database. It iterates over a list of customers (around 500).

I used ThreadPoolExectuor before to achieve parallelizing of a "for loop" and reduced my execution time from 10 minutes to 5 seconds.

I was not using any "await" inside those routes and everything was fine. Now I have a database call through Omar, for example, "await Customer.objects.filter(Customer.users.contains(mac)).first()".

This question might not be specific to Ormar, but to using the await keyword anywhere within FastAPI while trying to achieve parallelism.

Python complains and says I cannot use await in there. I Googled extensively but cannot find an answer to my problem.

Example Code:

async def customer_list():

 customers  =await Customer.objects.filter(Customer.users.contains(mac)).get()
 for customer in customers:
       if "Mr." in customer.name:
         //API call here
         await MaleCustomers.objects.update_or_create(**customer.dict())

Sequentially, waiting for the API call to complete and then the database write to finish is too slow. I would like to know if there's a way to parallelize each iteration of the for loop here?



Sources

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

Source: Stack Overflow

Solution Source