Learn Python's AsyncIO in 15 minutes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
how's it going guys in today's lesson we're going to be going over asynchronous programming in python and we're going to be using async io which is a package included with python the vanilla version so to get started we're going to go ahead and import async io and in the first example we're going to try to simulate an api request so we can see how this can actually be used in a real world project so actually i want to go ahead and create the api first so here we're going to go ahead and create a package called api and we need to go ahead and also import async io here because we're going to be using one of its basic features now the first thing you need to note is that every time you create an asynchronous function it's going to start with the keyword async followed by your function name which in this example just going to be called fetch data and it's going to return some data which will just say of type string for this example now we can go ahead and print something such as fetching data so we know that the api request is being called and then we're going to go ahead and create a delay so inside here we're going to go ahead and call a weight and a weight is a way of forcing the program to wait for the current function to finish because in asynchronous programming if you have a bunch of async functions and you call them all at the same time they're going to all work at the same time but one very important feature in asynchronous programming is being able to wait for a certain line of code and forcing the program to wait for that to complete before moving on in that asynchronous function so in this example we want to call async io dot sleep and we're going to put a delay of 2.5 seconds so a weight just tells the program that you need to wait for this before executing the next statement which is just going to say data fetched and down here we're going to return the api data so just by creating this async function we can now place it anywhere and it's going to run asynchronously which means if we want to create an api request we can do that and we can do other work at the same time so we're going to go back to our main.pi file and inside here we're going to go ahead and import api and we can close the sidebar so what we want to do inside here is now create a program that sends data to users after it fetches the data so to do this we're going to go ahead and create an async function which is going to be called send data and it's going to take a to user which will be of type string in this example and here we'll type in print formatted string sending data to and we insert the name inside there then we're going to call await again and async dot io sleep and we want to give it a two second delay now we can go ahead and print that the data was sent to the person that we sent it to so that's a very simple sent data function which of course you would replace with your own network request now we're going to make sure that this all runs asynchronously so we're going to call async def main which is going to be our main program and inside here the first thing we want to do is call our data which is going to be an await of api dot fetch data so it's important that we await this because this is the data we want to get back and we don't want to progress with the program until we've received this data that's why we use a weight here and we can also go ahead and print the data which is going to be of type data which is going to be the data now let's go ahead and try to send this to a certain user so here we're going to go ahead and call await and we're going to send the data to mario and let's go ahead and run this just to see what we've done so far so inside here to run an asynchronous program you need to call async io dot run and insert your master program which for us is just going to be main so just like that we can go ahead and click on run and it's going to fetch the data and as soon as it fetches the data it's going to display the data it's going to try to send the data to mario and it's going to send the data to mario so so far everything was quite synchronous it fetched the data it assigned the data to the data value and sent it to mario now that's not really the asynchronous programming part even though our code is asynchronous we haven't used it in an asynchronous way so what i want to demonstrate to you here is that we can send this to multiple users at the same time because if we just call a wait each time it's going to call it one after the other and that's not really what we wanted to do we want to send it to luigi and mario at the exact same time so how would we accomplish this and the easiest way to do this is to use the gather function in async io which takes as many functions as you want to insert so inside here we're going to send data to mario for example and we're also going to send data to luigi so we're going to use these two functions at the exact same time and it might be easier for you to create a list of functions that are asynchronous if you want to do that and then just pass the list inside here but for this example it's quite easy just to insert two functions and as soon as we run the program it's going to fetch the data and then we're going to retrieve the data and it's going to send to mario and to luigi at the exact same moment instead of sending it to mario first and then to luigi we were able to send it to both of them at the exact same time so that was the first example of using asynchronous programming now next we're going to go ahead and create a function that creates a thousand functions and runs them all asynchronously just to show you one more time how we can create lots of functions that run asynchronously so we're going to go here and delete all of that and we're still going to keep the main over here but what we're going to do as well is create a async function that is called kill time and it's going to take a number now we're going to print that this is running and we're going to insert the number here so we know which function it is and we're going to go ahead and await async io dot sleep for let's say one second and then we will print finished with the number so that's the only function we're going to create and it's quite simple it just tells us that we're running that function and that we're done with that function so we're going to run this 1000 times at the same time and we need to go ahead and create our async main function and inside here we're going to print that we've started the process now we're going to create a list of tasks which will be an empty array and then for i in range 1 to 1000 plus 1 we're going to go ahead and say list of tasks dot append and we want to append kill time plus the current iteration so we can tell which one we're on currently and before we do anything we're going to give this a two second delay so async io dot sleep and two seconds and we also need to call await await.asyncio.ganda and as i mentioned earlier you can place as many functions as you want in here but if you want to create a list of functions you need to insert the asterisk which says we're going to insert as many arguments as we want so here we're going to go ahead and insert the list of tasks and then we can go ahead and print done now when we go ahead and run this program it's going to say started and then it's going to run all of them at the exact same time and finish them at the exact same time then it's going to say done so here we successfully ran 1000 functions at the exact same time and as you can see it's quite a lot so asyncio.gather is crazy powerful in that respect that we can run as many functions as we want even in this demonstration where we ran 1000 it still was able to handle that so the sky is the limit now the final part of this tutorial will be explaining how you can use tasks in async io so that you can start them and you can cancel them and you can check for the result so to do this we're just going to get rid of all of this create an async dev main and inside here the first thing we want to do is create a task and the task is going to be async io dot create task and inside here you insert whatever task you want so for this example we're going to create api fetch data as our main task so as soon as you create this task it actually starts the task so right now it has been called as soon as you've created the task and what we're going to do is create a try and accept block and inside here we're going to add something called result which is going to be the weight of task and accept is just going to be empty for now we're just going to pass and let's actually go ahead and print the result as well so if we go ahead and click on run it's going to fetch the data and as soon as it retrieves the data it's going to say that we've fetched this api data so that's very similar to just creating a normal awaits except this time we inserted it inside here now the benefit of creating a task is that you have some extra context options such as task dot cancel now if we actually go ahead and run this nothing's going to happen and it's going to throw an exception but instead of accepting nothing which is very bad practice we're going to go ahead and accept async io canceled error and inside here we'll just type canceled request was cancelled and while we're here we're going to create another exception because i believe there's only two exceptions for this example and the second one is going to be a timeout error and for the second exception we're just going to print the same thing except you will say timeout request took too long so now if we run the program we're going to get the error that the request was cancelled so it tried to make the request and wild was calculating it said okay you're taking too long so we're going to cancel you and you can place this at any moment in your program as long as the task is running it will be able to cancel it in time and there's also another check you can say if task is canceled then you can do the following check this is going to return a boolean so here we can just say print task dot canceled which is going to return true but we also have to go ahead and call async io dot sleep and give it a 0.5 delay because sometimes this happens so fast that task.canceled is not going to be able to register the cancel in enough time and it's not going to be able to check for that so it's good to give it a delay sometimes so the program can catch up with what it's trying to check for so if we go ahead and run this we're going to get true because the task was cancelled since we cancelled it up here and it's going to give us the same error as from earlier now let's go ahead and remove all of this now another very good method provided by task is the task.done method and this returns true if we finish the task if all the code was executed successfully in the task this will return true so what you can do is go ahead and type in if task.done then we want to go ahead and print the task dot result if you try to print the task.result ahead of time and there's nothing there it's going to give you an error so we're going to go ahead and remove all of this and since we know very well that the task takes 2.5 seconds we're going to go ahead and call async io dot sleep for three seconds so here we can go ahead and type in a wait and as soon as it finishes getting the data it's going to print the data out here otherwise if we go ahead and just give it two seconds instead of 2.5 it's not going to have time to finish and it's not going to be able to print the data so this is a good way to verify that you actually have a result now the final method i want to show you inside here is a way to give this a timeout maybe you don't want to wait so long for the task there's a way to say that after two seconds we're going to give up and to do this we're going to go ahead and await async io dot wait for and as you can see inside here we can insert a task and we can insert a timeout so let's say after two seconds we want this just to give up now when we go ahead and run this program it's going to try to fetch the data but it's going to take too long because this function over here takes 2.5 seconds and the timeout is set to 2. so if you're making an api request you might set this to 10 seconds because you don't want the user to wait too long it might mean they don't have internet it might mean something else so the timer just gives you a way of handling requests that take far too long and everything that takes far too long is going to be handled in this accept block but anyways guys that's actually all i wanted to cover in today's lesson we covered how we can create some really cool asynchronous requests and those were essentially the basics of how async i o works i'm going to leave a link to the documentation down below because it's really well written and easy to read so in case you want to learn about some more particulars regarding this definitely check out the documentation but with that being said thanks for watching and i'll see you in the next lesson
Info
Channel: Indently
Views: 20,314
Rating: undefined out of 5
Keywords: code palace, cde palace, code palce, palace code
Id: 0GVLtTnebNA
Channel Id: undefined
Length: 14min 1sec (841 seconds)
Published: Tue Aug 09 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.