Replies: 1 comment 1 reply
-
You cannot reach parallel execution in this way. Each That is still quite inefficient code for orm, to update multiple entities you should use I assume that your API call is a REST API call to somewhere -> then you should parallelize this i.e. with httpx - you can check https://stackoverflow.com/questions/61537570/how-do-i-make-parallel-async-http-requests-using-httpx-versus-aiohttp-in-pytho |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
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():
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?
Beta Was this translation helpful? Give feedback.
All reactions