AsyncIO & Asynchronous Programming in Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] what is going on guys welcome back in today's video we're going to talk about one of the most requested topics on this channel which is async await in python or asynchronous programming in python so let's get right into it alright so let us first talk about what asynchronous programming is not asynchronous programming is not multi-threading it's also not multi-processing it is concurrent programming and we're not going to talk about the whole idea of concurrent programming and the whole coding patterns because that would deserve a series on its own we're going to talk about the basic principles and how we can implement those in python now let's look at a quick example here let's say we have a function one which is being called we have a function two which is being called and we have a function three which is being called so if this function the main function that those functions are being called in is synchronous this means that we're going to call function1 and we're going to then call function2 when function1 returns uh or actually terminates or returns this problem probably the right term here so we're only going to call function2 when function1 is done when it returns and then we're going to call function3 when function2 returns this is synchronous now if we use multithreading or multiprocessing that would not be the same as asynchronous programming because in multi-threading we would define three threads in this case and we would run all of those functions at the same time or roughly at the same time we will try to either run them simultaneously or at least create the illusion of a simultaneous execution this is not the goal of asynchronous programming with asynchronous programming what we want to do is let's say function one does something productive and then it requests some data from a database from an api or it just sleeps in general just for the sake of waiting if that happens we want to not waste any cpu time and we want to start executing function 2 even though this function has not returned yet but it's not doing anything at the moment so we can only run one task at the same time we're not doing any multi-processing or multi-threading we're doing one thing at the same time but if the function one is sleeping or waiting or being unproductive we can use that time to already start executing function two and maybe function three so that's the basic idea of asynchronous programming so in order to do asynchronous programming in python we need to import a library called async io so import async o and now in order to define a function as asynchronous because we're not going to just say the whole program is asynchronous just specific functions are going to be asynchronous we need to use the async keyword so we're going to say async def and a function name in this case i'm going to choose main we're going to have a main function which is going to be asynchronous and if we only have that main function of course the whole program is going to be asynchronous but that is the idea now let's do a very basic function here we're going to say print a and we're going to say print b and in between we're going to sleep but we're not going to sleep with time.sleep we're going to sleep with async i o dot sleep so async io dot sleep for one second now the important thing is here that we need to await this if we're calling an asynchronous function we need to do it with a task we need to await it or we need to do async io dot run in this case we're going to say a wait which basically means that we're going to wait for this statement to finish and before this this call here finished we're not going to do anything else this is important because in asynchronous programming as we learned we're just going to proceed if something uh wastes time you could say so in this case we're going to print a then we're forced to wait because we're saying await this thing here and then we're going to print b once the thing is done in order to run the main function we need to say async io dot run main and it's very important that we actually call the main function we're not just referring to it as we do in multi-threading we're actually calling the function so we use these parentheses here as well it's not like saying target equals main in a thread we're actually calling the function here we're not just referring to it so i'm going to run this and you're going to see a very basic program a one second and then b so that is not really asynchronous programming yet so what do we have to do here let's introduce a second function let's say we have an another asynchronous function which we're just going to call other function and in here we're going to print something else in here we're going to print one and then two one two and in between we're going to sleep let's say awaits async io dot sleep we're going to sleep for two seconds here now if we go ahead and call that function instead of sleeping here we can just go ahead and say await other function but that is not going to be very asynchronous because this is actual synchronous actually synchronous programming why because we're printing a we're awaiting the function which is the same as just calling it in a synchronous way we say okay don't execute this until all this is done so that's not really asynchronous but this is how you could do synchronous programming in an asynchronous function so what do we do however if we want to do something with the main function here we want to print a and then we want to call that function thus printing one but then while this function here is sleeping we already want to be doing that so we already want to be printing b and once this is done it's going to print two in order to do that we need to work with tasks so what we can do here in the beginning of the function is we're going to say uh task equals async io dot create task and we're going to create a task other function so we're going to call the function in here again and this task now is scheduled and basically this means writing it like that basically means that once we have some idle time we're going to call that task so i'm not even sure if it's going to call it if we just run this yeah it's going to call the beginning so we can see a b then it's done it's calling one but it can see here okay we're sleeping so i'm going to terminate why is it terminating because yes we call it but this function is the main function we're not awaiting this function this other function which means that once i reach the end of this function i'm done so i don't need to actually wait for you and wait for you to finish we're just going to skip you so what we can do here is we can say a weight task in the end so we can say away task and then you're going to see that we get a b 1 and then we're waiting for 2. so if i want to see how this actually works in an asynchronous way we can do it in between we can say okay print a then also sleep so we're going to say a weight async io dot wait uh not that wait sorry dot sleep for one second and what this means is that we're going to execute print a and then we're going to go to sleep for one second a second which means that this function now has idle time which means that this task now has time to be executed because up until now we had a b and then it started executing now we're doing a and this function is sleeping which means that we now have cpu time available which means that this task is now going to to use this chance uh and start executing itself so we're not awaiting it so we're going to see the result here we're going to say a 1 b so it's printing a it has idle time and this time this prints one it executes the function in the task but then this function also goes to sleep which means that it is now waiting this function is printing b and it then terminates which means that we're now no longer interested in the rest of this function if we want to be interested in that we have to say await task and then you're going to see that we get a 1 b 2. so this is asynchronous as you can see we're actually like if you if you look at the way we call this we actually say a b one two or if you say okay this is first then we're actually doing one two a b but the order is a one b because we go to sleep and the idle time is being used so that no cpu time is wasted now you need to take care where you're actually awaiting the task because i think if you await it up here you're going to wait for the for the whole task to finish so you're going to see first one two and when the task is finished you're going to see a b so this is not what you want all right so last but not least let's talk about return values what if my function returns something let's say other function is not just other function but it actually returns something in the end so i want to say return uh i don't know what let's say i return the value 10. now the problem here is that i can still run this but i don't get the return value so how do i actually get the return value from that task especially if i'm not doing it like that i need to await the task and once i await a task it's going to be done and when i await the task i'm going to get return value of the task so this task is executing other function but i don't know when this task is going to be done because here it says print a print b whatever and if i sleep here for like five seconds it's going to be done somewhere in between so if i do it like that you can see that i'm going to first c2 now the task is done and it returned 10 but where do i actually get the return value if i want to get the return value i need to await the task so even if i await it down here where it's already done because we saw we first get a 1 2 then it returns and then we get b i can still await it after the b statement and say a wait task and this await task gives me the return value so i can say return value equals away task and then i can just go ahead and print return let me just use an f-string here return value value was return value by the way let me just see one more time if i'm not blocking anything no i'm small enough down here perfect um but but that is the basic idea we have return value was return value so if i run this right now you're going to see that even though the task is actually done right now uh we can still get the return value afterwards so this what we get here is the so-called future in python it's like a promise in javascript it's basically just giving you the return value of the asynchronous task of course if i choose to await the task on any other position so let's say i do it what happens here let's say i do it up here so let's say i wait to task before i even start with a b then this of course also works uh it doesn't actually matter the point is you have to await the task the await keyword of course forces you to wait for the task wherever you put it so if you put it up here you're going to artificially say okay we need to do the task first if you do it in the end it doesn't matter where the task is actually done you're going to get the return value so if you do it uh here you're not going to have any problems because even if i change this to like 1 it doesn't really matter as long as i get the return value from the task now i need to use the await keyword because if that task doesn't finish because let's say this function is already done and i stop at this sleep statement here of course i'm not going going to get the return value so in order to get any return value i need to await the task this is obviously the case because if i don't await it until it is finished it's not going to return anything so this is how you get the return values from asynchronous functions so that's it for today's video hope you enjoyed i hope you learned something if so let me know by hitting a like button leaving a comment in the comment section down below and of course don't forget to subscribe to this channel and hit the notification bell to not miss a single future video for free other than that thank you very much for watching see you next video and bye [Music] you
Info
Channel: NeuralNine
Views: 135,756
Rating: undefined out of 5
Keywords: asyncio, async, await, python, asynchronous programming, python async, python await, synchronous programming, coroutines, concurrency, concurrent programming
Id: 6RbJYN7SoRs
Channel Id: undefined
Length: 12min 28sec (748 seconds)
Published: Thu Apr 29 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.