How To Use TASK GROUPS In Python 3.11 (NEW FEATURE!)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
before you watch this video it's important to bring up that you should have some understanding on async iO to get a really good grasp on this feature because even if this might be a new feature in Python 3.11 if you don't know anything about async IO you're just going to be completely lost during this lesson so I recommend you look into async IO before following along with this video but if you are up to date with how async IO works and how to create asynchronous tasks then we will just go ahead and jump into a new feature that was introduced to us in Python 3.11 and that is the task group so right now I have two asynchronous functions one is a function that reads a file and as you can see we have a file name of type shrink we try to open that file name as a file and we try to read the data from it and we want to return the data that's inside that file so a normal reading operation and then we have a dummy function called Fetch data which allows us to put some data inside here of type integer so that's just some fake data we're going to query and if the data is equal to zero we're going to raise the exception that there's no data found and otherwise we're just going to return that integer as the data now I went ahead and imported async IO because we will be using async IO to use this task group and we also have the main entry point and the async io.run method which allows us to run that main asynchronous function now prior to python 3.11 you would go ahead and do something like this if you want to create many tasks you'd go ahead and say asyncio dot gather and inside here you would insert your core routines so maybe we want to fetch the data for the value of one fetch the data for the value of 2 and maybe we also want to open a file so we will Type in read file and the file name will be image.png which doesn't exist so that will throw an error and then we can do something such as await tasks to make sure that all the tasks complete before moving on with the program and we can print done here as soon as that is finished but now if we go ahead and run this we're going to get an error immediately that there was a file not found error and that is for image.png and the downside of using.gata is that if we have to create another request such as fetch data with the index of 0 and we go ahead and run this we're not going to be able to see the error for fetch data at zero because it's going to raise an error before we even reach that so we're not going to know the other errors until we actually deal with the previous era for read file now a simple solution for this was to go ahead and set return exceptions to true and now when you go ahead and set a value of results to await.tasks and print the results you would get all the results back even if they contained an error so here we get data 1 data 2 for the first two tasks and for the second task where we have an error it's going to return to us the file not found error with the error inside it and also for the fourth task we have an exception that there was no data found because we provided zero so this is how we would do it in the past now it's time to actually jump into the python 3.11 version which has to do with task groups so let's get rid of all of this since we won't be using it in Python 3.11 and what we're going to do inside here is try and use the keyword async with asyncio.taskgroup as TG so that was the equivalent of creating The Gather method and inside here we can go ahead and call TG dot create task and we can start our task just as we would with gather so here we can go ahead and type in Fetch data one and we can duplicate that say two we can say zero and we can also try to read a file so read file and this time we'll type in banana dot jpeg and at the bottom of this we want to print that everything went according to plans we'll say done and that's not going to happen because these two are going to throw errors but the advantage of using this task group is that we're now going to get these exception groups back instead of just one exception at a time so we can handle multiple exceptions in case multiple of them happen inside the task group in gather we would only get one at a time but here it's going to try to give us all of the errors that might occur so here let's type in some of the possible errors that will appear such as accept file not found error which is going to happen with banana.jpg as EG and we will print the exception group here and we will also accept all the other exceptions which I manually created so accept exception is going to catch that as well so accept exception as G and we will print the exception and what we're going to get are unhandled errors in a task group and this is my mistake because I forgot to use the exception group syntax so here we need to add a star to each one of these because that's going to tell the program that if it encounters a file not found error or an exception that we should group it into an exception group so we can run this one more time and we're going to get the unhandled errors for each one of these blocks it's going to split them and the reason we don't get anything detailed is because we have some helper methods such as exceptions that make it clear that we want to get the exceptions back so we can run that one more time and here we'll get the correct errors for each one of these exceptions so something nice you can do with this for example is go ahead and type in for error in eg.exceptions print the error so you can handle each one of the errors in the file not found error separately and if you run that we only have one at the moment which is no such file or directory for banana.jpg but if you have two of these and you type in apple.jpg so we try to read both of these files we're going to get both of these errors printed inside that exception over here so we can handle both of those respectively but they will be grouped into the same category of file not found error and the same thing goes for exception if we have some random exceptions such as fetching data at the value of zero it's going to throw that General exception so it's going to paste it down in that so the main advantage of using this task group over gather is that we get a better understanding of the errors that we're encountering with the exception groups and we can still use the values as normal we can go ahead and type in for example task equals and down here we can then use that task or develop value of that task as you normally would because as soon as the width block is finished it's going to give us the result so if we go ahead and type in print task dot result and run the program it's not going to give us anything of course because there was an exception that Rose in the try and accept block but if we get rid of those errors and we just try to get the task back you'll see that we can use that task as normal as soon as we're done with this task block so as of python 3.11 it's now recommended that you use the taskgroup over dot gather that's just how things are now it gives us better access to exception groups which if you are familiar with them can make your life easier if used correctly otherwise if you need your code to be compatible with previous versions you're going to have to continue using dot gather but with that being said that's all I wanted to cover in today's lesson if I missed something please do let me know in the comment section down below so I can learn from it but otherwise with that being said as always thanks for watching and I'll see you in the next video and for those of you that made it all the way to the end of the video I'm going to show you exactly what's under my beanie more hair
Info
Channel: Indently
Views: 11,393
Rating: undefined out of 5
Keywords: pyton, pyhton, pythn
Id: 8KyIK7srLds
Channel Id: undefined
Length: 8min 1sec (481 seconds)
Published: Sun Nov 27 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.