Join Async Tasks | Tokio Join | Rust

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys in this video we'll be talking about join async and rust so when we have multiple tasks and we want to execute them concurrently and wait for them we use join and rust before we move any further go to your cargo TRL file and make sure you have the Tokyo executor dependency added with the latest version and the feature as full back to your main method let's create two dummy task so we'll save uh Asing FN download we pass some URL string and it returns some string content and here uh quickly let's add some dummy uh download mechanism so we'll say starting download and URL uh then let's sleep for some time make sure to use sleep from Tokyo and not from the standard threade library then duration from seconds let's leave for 3 second and then we'll say uh down downloading completed and then finally some content so we'll say HTML HTML and then let's say uh dummy content okay so this is our task which runs and downloads something from the internet and we mimic it with some sleep so just to mimic the behavior and then Asing fun and then we have import file it takes some path string and returns us some string content so let's copy these lines and quickly make the changes and here we'll say importing file and pass the path let's leave for 6 seconds here and say then we'll say import completed and here we'll say uh file content is J one for example so guys we have two tasks one is importing the file and other is downloading we can easily run these tasks concurrently because they are not dependent on each other so let's say uh we have some address and here we'll say uh https example.com and then we'll say uh local path uh which is uh maybe example SL uh sample.txt and then what we'll do is uh first of all since we want to use join we have to make our man as async and as we saw in the last video for men to be async we have to use Tokyo man uh the executor here and then what we can do is let uh Network content comma local content and then we can do Tokyo join and here we can call our task so we can say download and pass the address and second is uh our import file and then pass the uh local path so since it is looking for string uh so we'll say two On and Here Also two on so since it's looking for a string and it this is slice type so that's why we have to pass uh two on uh to make it uh string now we have Network content and local content so uh let's print them so we'll say print uh task completed or let's say task completed and let's pass the result so task one network content task two to is local content and guys this should not be just two this can be any as much as you want but make sure your tasks are not dependent on each other here we have just joined two task now if I do cargo run so you can observe both started downloading and complete uh importing file but as you can see downloading is completed but still we don't see response we only see when both the tasks are completed so again repeating uh Let's do tiar let's do cargo run you can see downloading started import file started but since in downloading we are sleeping 3 second and here for 6 second so when both the task gets completed or if you have n number of tasks here when all of those tasks completed that's when we get the result so basically how Tokyo join works is it wait for all the tasks to complete it and then return the result so that's it for for this video guys I hope you guys understand the concept I will catch you guys in another video with another interesting topic until then thanks for watching
Info
Channel: Semicolon
Views: 98
Rating: undefined out of 5
Keywords:
Id: k7nmfd-3p9A
Channel Id: undefined
Length: 5min 8sec (308 seconds)
Published: Sun Feb 04 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.