1

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?

  • `await` does not imply parallelization, just that you give up your processing time while other, async-compatible code runs (such as code that depend on io). If your api call isn't written with async functionality, it won't be able to give up any time while waiting for data to become available from the api. If you control the API client, using a httpx or a different async compatible http client might help. – MatsLindh May 13 '22 at 13:02
  • @MatsLindh I am not going for true parallelization, more like threading where the CPU jumps between tasks. The thing is I am able to put each iteration in a loop when the code inside doesn't use await keywords. Have you ever used FastAPI to implement such things? – Shiv Kokroo May 13 '22 at 19:32
  • Yes, but for async to "jump between tasks", the currently running code has to hit an await, otherwise time won't be given up to other lines of code. If you await a function that has a `time.sleep()` inside, nothing will happen (in no other parts of your code either) until that sleep is finished. However, if you use an async compatible call by doing `await asyncio.sleep()` instead, other async code will be able to run until they return or await again. If you want to parallelize code that isn't async compatible, use a threadpool as you've already done. async isn't another way of doing threading – MatsLindh May 13 '22 at 20:16

0 Answers0