em1208/adrf

Best practices for partial async implementation in DRF

gschurck opened this issue · 6 comments

I have currently a Django API project built with DRF.
I would like to use async for some endpoints to optimize long API calls.
What are the best practices in terms of performance ?
I suppose that I have to change my server for Uvicorn ?
Will the normal sync views still work correctly ?
Thanks.

Hi @gschurck, apologies for the delay in the reply, I just noticed the open issue. You can mix and match sync and async endpoints, as long as the version of Django you are using is supported and adrf is installed correctly, and there is no need to change your server to Uvicorn for async support, I believe, but you can try different configurations and report here if you want. I have not made any performance/configuration comparison yet, but that's something in my mind that I would like to do when I have time. I hope this helps!

Hi @em1208! I was looking for different possibilities to make async views on DRF, and run over your comment here. I'm trying to understand it. Why is there no need to change the server to Uvicorn? Shouldn't the app run with ASGI in order to support asynchronous code?

Hi @mateokurti, I believe you are right, the app should run with ASGI to support asynchronous code, Uvicorn is one of the available options.

Since this topic is related to my question, I'm just asking it here. @gschurck did you manage to optimize those long API calls? In my case, I made one endpoint async as it takes ~10 seconds to be finished. However, my other sync endpoints stay blocked until the request on the async endpoint is finished.
Note that I'm running the app with Daphne ASGI, so it only has 1 worker in contrast with uvicorn which can be run with multiple workers. It just doesn't make sense to me why an async view should be a blocker for others. What am I missing?

I didn't really find for now, I am afraid that all the endpoints have to be async to be handled concurrently ? But not sure.

em1208 commented

PR #19 has been merged which add support for mixing synchronous and asynchronous handlers.