DEV Community

Muramoto Hideyosi
Muramoto Hideyosi

Posted on

Will django convert all the view to async view?

From Django3.1, it provides the async view for better performance.
I am thinking about converting my previous project to Django3.1 to use an async view.
I am not sure it is good or not.
I Need help with it.
Best regards

Top comments (3)

Collapse
 
frenetic profile image
Guilherme Medeiros • Edited

It can or cannot be good. It depends on what your views do.

At first, adopting async will force you to adopt ASGI. For most scenarios this is just a simple config and/or changing your server stack.

Then you have to ask yourself: why do I want async? To serve more requests on the server or to speedup certain requests?

Basically, all Django apps do "get request, query database, return response". Since querying database is slow, you can get your worker to start serving another request while it awaits for the query to fully execute. This way, you will have more concurrency on your server.
At first glance this is great. You will get more requests being served with a single server/container.
However, this can be a shot in your foot. After querying the DB, you want to return a response. Django will turn the SQL it got into Python objects. You may want to do serialization and transformations. All of this will add up to your memory consumption. Depending on how you serve things (bare metal vs VPS vs Containers), you may have not enough memory, killing that specific worker. It will error one or more requests.
So, if you are looking to serve more things, you will have to look for optimizing your views and queries and fine tuning your server setup.

Speeding up certain requests is easy and probably wont lead you into troubles. If your views perform more than one DB query that do not depend on each other, or queries another API, or do anything else that is slow, you can do them all in async fashion. By doing this you will be shaving off some miliseconds or seconds from your request.
However, the same may happen here. You may error some requests due to concurrency. So, fine tune your server setup.

You may argue with me that other languages/framework can do more with concurrency. However, at the end of the day, they all need you to test and fine tune the server setup. It all depends on what each request does.

Collapse
 
steelwolf180 profile image
Max Ong Zong Bao • Edited

Yup I will label it as over engineering especially you can focus on the more important parts like documentation, simplifying the deployment process or refactoring your code or adding test cases to test assumptions. As those are more useful than transition into async request.

Collapse
 
steelwolf180 profile image
Max Ong Zong Bao

When your using that you might need to ask yourself. Why do you do it? Cause if it doesn't make sense to do it, just don't do it.