Introduction to async views in Django | async/await in Django views

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys this is luke and i'm here at the official django documentation where we can see some informations that from django 3.0 we have now asynchronous support and from django 3.1 we have async views so this is exactly what we will be talking about today we will create a new django 3.1 project in order to understand how async views work what are the benefits of using async views over ordinary views and we will do it on some very very simple examples so at the end of the video you will have a better feeling on this particular um one thing that i would like to mention before we get started is that i would highly recommend to read this documentation as you can see it's not very very long probably one cup of coffee 15 minutes and you will be done with it and the second thing is that this isn't a async io tutorial so i won't be deeply covering such topics as core routines or event loops if you would like to find out more on async io i would highly recommend you go through the official async io documentation but this isn't mandatory for this and this particular video you will be able to follow along without having the knowledge on async io all right guys so with that being said let's get started [Music] all right guys so before we actually get started as usual let's have a look at what have i done so far but with one major difference this project is on github so i use your advice thank you for for this and other popular and i'm using here with my fingers the quotation marks when i say popular popular videos on my channel will be uploaded within days probably by the end of the week all of them or most of them will be uploaded to github and of course i will paste the links underneath the description of those particular videos okay um let's move on so i created a async project this is a project that uses the version of django 3.1 and here we have two application movies and stories and i added stories and movies to the installed apps list so besides of that i haven't done anything over here okay so in the urls py i haven't done anything as well i just basically worked with the models py and the admin py for the movies and the stories so we have very simple models for the movies one field name and this is a char field with the string representation of self name and for the stories the same thing one field uh models char field string representation self name and this is basically it both of them are registered in the admin so we can now take a look at what's over there and i basically created two movie objects and two story objects okay so here we have um in the movies we have lord of the rings and interstellar and in stories we have snow white and seven dwarfs and beauty and the beast all right and this is basically it so the next thing that i'm going to do is to head over to the main project folder and here i'm going to create a views py file views py okay and i'm going to begin by defining a home view so um let's write def home view and this will take in a request and let's return and i'm going to import http response so from django http we want to import http response and here i'm going to return http response with hello world all right so this is going to be the first view that we are going to use and then we are going to have um let's call it maybe a main view and a main async view so we are going to define them in just a second for now i'm going to write them down so we'll have main view and this is going to be a synchronous ordinary view let's call it like that and it will take a request and for now i'm going to write down pass and we will have a def main view async and this will also take in a request and we will change this function view a little bit later and for now i'm going to put in pass as well so we will begin from here we will begin by defining those views and what i'm going to do next is to head over to the urls py where i'm going to import them from django sorry not from django but from dot views we want to import our home view and our main view and our main view async and here we will create some paths so the first one is going to be the main one for the home view and i'm going to set the name home view then i'm going to put in the main view so this is going to be a synchronous view so i'm going to put in sync and then main view and the name is going to be sync main view and the third one is going to be this main view async so let me just copy this put it over here we will have some changes of course instead of sync we will have async and here i'm going to put an async as well and the main view async will come over here all right so right now what we can do is to return to our views py file um i'm not going to create a separate utils py file for the helper functions i'm just going to keep the views over here and at the top we will define some helper functions i'm going to call them funcs um and we will do this in in just a second but for now let's import time and let's focus on this main view okay so here i'm going to create a variable called start time and this is going to be time dot time so it will measure our time from here and then we will have here we will put later on some helper functions but right now we can put in total and this is going to be our time dot time minus the start time okay and we can simply print out the total and let's also return the http response which will be sync okay so now we have start time we are calculating the total we are printing the total let's maybe add here total okay sorry total let's save this and let's go to check this out i'm going to see if the server is running it's running so let's go to the browser and we want to go to sync and we have sync let's see at the total is a very very small number at this particular moment so i think it's a very good time to work on some helper functions we will define some helper functions we will take a look at this main view again and then we will take a look at the total time and then we will think how we can improve it with the async approach with this main view async which will basically do the same thing but a little bit better so let's define the helper functions and we will have get movies function and get stories functions so i will define them at the top the one will be the first one will be get movies movies and the other one will be get stories okay and yeah let's start by writing a print statement here we can put in pre pair to get the movies so here we are preparing to get the movies then we can put in time sleep two seconds so we will block the execution for two seconds and then we will go to another block where we will define a query set and we will write movie but we need to import our movie and story so we want to write down from movies dot models we want to import our movie and from stories dot models we want to import our story so here let's put in movies movie objects all and let's print out our query set and then let's move to the next line where we will print um a confirmation that got all the movies alright so now i'm going to take this and put it over here and prepare to get the stories time sleep 2 then we have qs story instead of a movie and then got all the stories but this time the time sleep won't be two seconds let's do it five um okay so now what we need to do is to bring get movies and get stories into our uh main view so i'm going to put in get movies and then get stories so i'm going to save this i'm going to head over to home and now i'm going to head over to sync and it's loading it's loading it's loading and how long will do you think it's loading let's take a look at what do we have here we have about seven seconds okay so if i refresh one two three four five six seven eight or yeah it's like about seven seconds of loading and let me do it one more time and let's see what's going on over here so we have prepared to get the movies we are getting the movies here are the movies we have a confirmation that we got all the movies and then we are jumping to uh get the stories so we are preparing to get the stories here are our stories and here is a confirmation that we got all the stories and in total it took us around seven seconds okay so this is the uh ordinary approach now let's take a look at the improved approach okay so we will be talking about this main view async so first of all let's go back to the documentation and here we have here we have that if you want to call a part of django that is still synchronous like the oram you will need to wrap it in sync to async decorator okay so i'm going to grab this because we are going to import this wrapper and i'm going to put it at the very very top and i didn't copy it sorry about that um let me try it out again and let's paste it and now what i'm going to do is to copy this as well and put it over here so right now we will have get movies async and get started stories async and nothing and nothing else will change over here but we need to use the sync to async decorator on them so i'm going to apply it over here and over here okay so right now we can change this function to be async and what we need to do over here is to put in at the very front async and i don't know why it's not highlighting the nice way as it is on my laptop but for now i'm not going to worry about this and then i'm going to grab this code this line of code put it over here and the next step will be to go all the way to the top and import or we can do it in one line next to time let's put in async you all right and right now we can create a task so i'm going to show you two ways how we can use for example those helper functions to work side by side the first one will be task to define tasks so task 1 will be equal to async yo and then ensure future and here we will put in get movies async and the other one will be task 2 and this is going to be a cinco and ensure future and then put in get stories async all right and then we need to await async your weight and put it as a list task 1 and task 2. and then we can grab in the total and print the total and actually return the http response so here we just need to change it to async okay so we will walk what's going to happen what's happening over here but first let's see the actual result so i'm going to um save this um in the urls py we have it defined as async so actually let's jump into async right now async and it's loading it's loading and we have async so let's take a look at the performance now as you can see we have five seconds so i'm just going to grab this put it over here so we can see the difference here we have five seconds and if we go to sync we have seven seconds and we will understand why in just a second let me put it over here okay so let's compare these two so i'm going to um first of all let's remember ourselves how the the ordinary main view worked so we were um where is it let me run it one more time so so we are preparing to get the movies here are our movies and then we got all the movies and then we are preparing to get the stories here are our stories okay here are our stories and then we got all the stories so we prepare we were preparing the movies we got the movies and then we simply have a confirmation that we got all the movies and then we took care of the stories but if we take a look at async right now it will be a little bit different so here we are preparing to get the movies and preparing to get the stories and then we have all the movies and then we got all the stories and the time is a little bit over five seconds and the reason why is with this construction with this async await we are running this those tasks side by side okay so if we take a look at um the get movies and get stories here we have time of five seconds okay so basically uh this and this works side by side and after five seconds we have a confirmation that we got all the stories and before of course we have a confirmation that we got all the movies so in this way we are saving uh over two seconds and now let's have a look at this um this this particular async function with the async 08 so we can understand what actually async 08 does so basically change the changing this uh main view async function to async 08 we are creating a core routine and the most important thing over here is how this works so with async await we are um here we wrote with the away that we are waiting for task one and task two and we created those tasks with ensure future so those two tasks are working right now side by side and this block of code and this block of code won't be executed until we have task 1 and task to finish and of course the http response won't be executed as well that's why we have this this wait time okay so if we um go to the browser let's let's see if it's five seconds so i'm going to refresh one two three four five yeah probably something around five seconds but here we were already at async so let me go to home and then go to async again so one two three four five yeah something like five seconds okay and then if we go to sync we have one two three four five six seven eight yeah so it it takes longer and one more time with this async 08 and this await a sync your weight we are basically taking those tasks that we created with ensure future and we are running them basically side by side and we are awaiting for them to be finished and until they aren't finished we aren't executing those lines of code so this is why we have this await statement and this is of course one way to do it i'm going to show you now another maybe simpler way for sure it will be shorter so i'm just going to comment this out and instead of defining the tasks we will be using i think your getter so i'm going to put in a weight and then async your gather and we are going to put in get movies async and then get stories async all right and this will work in the same way so i'm going to save this i'm going to head over to home and then put in async one two three four five and we have the same results but instead of creating three lines of code we basically and just put in one single uh line of code where to the getter we passed our helper functions okay so one thing that it's very important is that if you want to simply proceed with the execution of the code you need to create tasks like we did over here and simply wait for them to be finished or you can simply use this async your gather which will do it basically uh for us okay so this is this is uh how it's working so we can see the result that with the async it's better but there are still a lot of things that uh we can do this is just a basic introduction and frankly speaking i'm very excited about the the future i'm very excited what kind of functionalities will the new versions bring how they will make our lives easier and our code works simply better so i hope uh for all of you that didn't quite get this async await and how async views work this concept is a little bit more understandable i will finish off over here um thanks for watching um this week i'm hoping to publish three more videos so one will be about custom comments this will be quite a fun and easy one and then the other one will be um the i think 14th part of the social network project and the final one will be the fourth part of the django with artificial intelligence project alright guys so uh hope you enjoyed this tutorial have a great day and bye bye
Info
Channel: Pyplane
Views: 18,091
Rating: undefined out of 5
Keywords: django, async await, async await python, django async views example, django async, async await django, django 3.1 async, django 3.1, async views in django, django asyncio, django async function, django async view, django async performance, django async tutorial, django async view example, django 3.1 async views
Id: YneIutRhmgo
Channel Id: undefined
Length: 25min 23sec (1523 seconds)
Published: Thu Aug 27 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.