Async Django running on Uvicorn - multithreading issue #9034
Unanswered
musanaeem
asked this question in
Question & Answer
Replies: 0 comments
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.
Uh oh!
There was an error while loading. Please reload this page.
-
I was looking into Async Django, trying to find out benefits and shortcomings of using ASGI instead of WSGI server with async and sync view when i found that ASGI was creating multiple threads for multiple requests.
I set up a demo project with asynchronous functions using asyncio.sleep() to add delay. Using ASGI when i make 20 simultaneous requests for the asynchronous view, it makes 20 threads (instead of 1 thread handling the processes by setting aside the one that has delay and working on the one that is ready to be processed). I am not using any middleware or extra app that may have any synchronous code to cause this issue. More over i tried uvicorn with FAST API and it worked as expected (a single thread for async code).
According to the documentation (https://docs.djangoproject.com/en/4.2/topics/async/), "If there is a piece of synchronous middleware, then Django must use a thread per request to safely emulate a synchronous environment for it." However there is no synchronous function in my project and yet I am still facing the issue of 1 thread per request.

Here is a link to the minimalistic dummy django code I'm running using the function "uvicorn async.asgi:application --host 127.0.0.1 --port 8000 --workers 1" on a unvicorn server: https://github.com/musanaeem/Dummy-Async-Django
Does anyone know how I can run Async Django using Uvicorn such that I have 1 thread dealing with multiple simultaneous requests ?
Beta Was this translation helpful? Give feedback.
All reactions