1

I haave a challenge with async/await blocking other requests in FastAPI. My code used to work well calling requests in def functions. For instance:

someRequestsApis = APIRouter()

someRequestsApis.get("/something/")
def getThings():
   return list(things.find())

Things would work well in this format. Now, I find myself for some reason having to switch from pymongo to AsyncIOMotorClient. The thing is I cannot run the motor database apis in my requests without using async/await. What used to be:

def getThings():
   return list(things.find())

is now supposed to be:

async def getThings():
   return await things.find().to_list()

Even the background tasks that are supposed to handle some of this logic have to be asynchronous, yet requests get blocked. I have searched for answers on this issue, yet the answer seems to suggest that the solution has to be to remove async/await in my requests and since I seem not to have such a luxury what is my work around strategy for such?

Chris
  • 18,724
  • 6
  • 46
  • 80
DUMBA
  • 159
  • 2
  • 10
  • async doesn't parallize requests, so if any of these endpoints have some code that isn't async compatible could be the issue. How large is the result set returned by `getThings()`? – MatsLindh May 06 '23 at 21:48
  • @MatsLindh well I am processing quite large datasets and we are talking from 100 000 to upwards. So would you suggest. Should I rather return to pymongo???? – DUMBA May 06 '23 at 22:01
  • for this is an issue I cannot afford to have – DUMBA May 06 '23 at 22:02
  • Well, that depends on why you switched away from pymongo in the first place. It's hard for anyone else to make that analysis - just remember that if you're going to use async, you'll have to implement it all through the stack. If `to_list()` with 150k rows and then working with those takes a lot of time, you'll still be spending a lot of time processing those rows. In production you'll usually run multiple workers, but a CPU intensive task will block other coroutines from doing anything. – MatsLindh May 06 '23 at 22:42
  • @MatsLindh thank you so much. I think for now will have to revert back to my old code till I familirize myself well enough with async for me to use it – DUMBA May 06 '23 at 22:49
  • @Chris thanks a lot your answer has served me from weeks of headaches. May you help me on how do I exactly make this a duplicate question – DUMBA May 07 '23 at 21:42
  • have duplicated thanks – DUMBA May 07 '23 at 21:53

0 Answers0