 Right, so hey guys and welcome back to another Python tutorial. So as promised in the last tutorial, this tutorial is going to take you through the new way of how you use multi-processing to pretty much run a function multiple times at the same time. So instead of having to run a function in a synchronous method, which is one after the other instruction, we're just going to be running it at one time. So it's going to help us save a lot of time. So if you haven't already watched part one of these, this tutorial, which is using multi-processing in Python for beginners, it's going to be linked in the description. So I recommend you go ahead and watch that first. If you already know how multi-processing works and are just curious about another method of how you can implement it, you can carry on watching. So first of all, what I'm going to do is go ahead and open up Visual Studio Code. And then what I'm going to do in here is create a new file and then save this as newMult.py. You can call it whatever you like as long as you have a .py extension, which is a Python file. So I'm going to save it on my desktop and then let's start coding. So first off, I'm going to zoom in a bit so it's easier for you guys to see. And what I'm going to do first is instead of doing import multi-processing, we're going to do import concurrent.futures. Cool. So this is the module that we're going to be using. It's called concurrent. And then the futures class is the bit that we're going to be using. So what we want to do after that is also import time so that we can track the total time of execution for this program so that we see whether we're actually saving any time or not. So first off, what I want to do is actually go ahead and create a function that's going to sleep for a bit. So quite literally it's going to sleep for a bit. And then in there, I'm going to pass an argument of how many seconds I want it to sleep for. Now I'm going to print an f-string, which is going to say sleeping. And then it's going to display how many seconds the user has asked it to sleep. And then it's just going to say second bracket s, second bracket s. Cool. And then we need to use the time dot sleep and then use seconds, whichever amount of seconds the user passes through. And then lastly, we just return, we say done sleeping. Now as you may have already noticed in the last tutorial, we had done print done sleeping. But in this one, we're actually going to be using done sleeping because after the program or the function has been executed using our concurrent pull executor that we're going to create in a second, after it's pretty much done executing this in at the same time, what it's going to go ahead and do is it's actually going to return the result of the function. So whatever the function returns. So that's why we're using a return. Cool. So what we want to do is type in with concurrent dot features dot process pull executor, which is kind of like an object that act as a queue or holds all the functions that you want to run at the same time. And we type in as executor so that we can just call it executor without having to call it this entire name here. We can just call it executor the next time we need it. Now as before as well, we need to use the if name method because that's required as standard when we're using multi processing. And then we do colon and let's create a variable called f1, which is going to pretty much store the object that we need. So we're going to do executor, which is the object we created a second ago, dot submit. And then in the executor, we can submit our function, which is called sleep for a bit. And then we can pass in our argument, which is going to be how many seconds we wanted to sleep. Let's get to sleep for, let's say one second. Cool. And then once we're done, we can just print f1 dot results, which is pretty much the result that's going to be returned. So that's whatever value is being returned by the function, just in this example, it's just going to say done sleeping. Cool. So what I want to do next is go ahead and create a finish variable here. Finish equals time dot perf counter, which is pretty much going to count how long this program took to execute. And then we're going to say print finished in time and then comma finish. So this is going to tell us how long the entire program took to execute. So what we want to actually prove here is if we were to run this function called sleep for a bit twice, it would have taken a total time of two seconds and a little bit more. So if we run it using multiprocessing, it should technically run in one second in a few more decimal places. So if I want to run that again, I'm going to just call this F2 and then equals that to executor dot submit and then sleep for a bit comma one, because I want it to sleep for one second again. And then we're just going to do print f2 dot result because it's going to return done sleeping to. So now that I've got all of this done, I'm going to go ahead and run this program to actually see if it's worked how I want it to work. So as you see right here, it says it has finished in time and then it says one second and a bit more. Obviously, as I said, it's going to have one second and a few more decimal places because it needs to do the imports and run the other instructions that we have apart from the function running twice. Now if it didn't work properly, we would have got a finished time of two seconds and a bit more because technically sleep for a bit function, which is being run twice by the way, is sleeping for one second each. And if it was being run in our normal way, which is asynchronous type, it would have taken two seconds because the time dot sleep itself takes a second. So that running twice is two seconds. And it would have a few more decimal places for the other instructions to be run. Also, if you notice, we have the value that is being returned by a function, which is called sleeping once, which is what was it called? Let's see. It should have been called done sleeping on thinking, let me go and check. Yeah, so it should have been called done sleeping. Let me go ahead and check if that actually shows up. So let's run this up. And do we have it here? So finished in time, finished in time. It just prints out the time. Then it says sleeping one second. OK, some reason it hasn't showed up, but let's see why there is. So if I get rid of this for now, let's get rid of that. Oh, it's because basically the F1 dot result is meant to be a function. So we need to use the two brackets right there so that it's a call to the function. Otherwise it won't work. So make sure you have F1 dot result with two brackets opening and closing bracket. Hopefully it works this time. Let's go ahead and see. And as you see right here, once it's done executing the two functions, it says done sleeping, which is the result of the function. So after everything is done, it returns done sleeping. And hence the pull executor or the process pull executor takes the result from the function and then prints it up twice because we're using two pull executors in this case, using the dot result to find out what the function returned. And then lastly, we have a little line that says how long this whole program took to run, which is the correct time because it's one second and a bit more. Anyway, guys, that was it for today's tutorial. I just wanted to make it short and just show you guys the most recent method of how people are using the multi-processing module to run two different functions or one function multiple times at the same pretty much time by saving a lot of time. So this approach is usually used when you have to do a lot of processing and use the same function with different parameters to calculate different values. So it might sound a bit complicated, but if you're looking up a tutorial for this, you probably know what you want to use this module for anyway. So I hope I was able to help you guys understand how this works. If you guys feel like you have learned something new from this video, please do make sure to drop it in the comments as I would understand that I was able to explain it properly. If you guys would like to donate to the channel, you can do so by either signing up as a patron using the Patreon link in the description or purchasing a Super Chat emoji or a highlighted message when this video premiers. Anyway guys, like always, also make sure to share the video, follow up the socials and join the Discord for a lot of fun. And I will see you beautiful faces in the next tutorial. Peace!