Hello. In the upcoming Django 3.1, an asynchronous request stack will be introduced in Django. For a project I have in mind, the large majority of my code will be synchronous. There will only be tiny bits of async code here and there for things like API calls.
I understand that you should deploy using an ASGI server when using asynchronous code in Django, to gain the performance that the async code brings. However if the large majority of my code will be synchronous, should I still deploy with ASGI, or with WSGI instead.
Which will perform better for code that is mostly synchronous, however has tiny bits of async code here and there?
If I deploy using ASGI, and the majority of my code is synchronous, will I still get the same performance than using a WSGI server for this synchronous code? Or is one faster than the other? Thanks.
If you’re making these API calls “in the background,” not within a web request, there’s no advantage to ASGI or async views.
No, there’s a small overhead for using sync views under ASGI
@adamchainz Ah ok. So if within the web request, I make an API call, then I can leverage Async Django right and ASGI right?
Also when you say “small overhead” when using ASGI with synchronous code, what do you mean by this? Thanks, Eesa
Yes. If you have an async view under an entirely async middleware stack on Django 3.1, then if you make an async API call with e.g. httpx, it won’t hold up a thread.
ASGi requests are sent to a threadpool to run synchronous middleware/views, and this switching costs time.
@adamchainz Ah ok thanks. So if I have bits of async code here and there, should I just use WSGI instead then? Which do you think will be more performant in this instance?
Use WSGI and measure your performance. When you need to optimize, consider async code as one tool in your toolbox.
@adamchainz Ok thanks, this helps a lot.