so uh yeah I'm going to talk about multi-threading in Python today multi-threading is used to perform multiple tasks concurrently think of it like we're multitasking we're performing a few different actions at once for example I could study and listen to music and eat something at the same time multi-threading is good for Io bound tasks IO meaning input output such as reading files or fetching data from an API things that may take some time to do and we don't know when it's going to end exactly to use multi-threading we'll import the threading module import threading we access the threading module then call the thread Constructor and pass in a Target function what we'll do for this demonstration let's say we have a bunch of chores to do we have to walk the dog get the mail and take out the trash let's define some functions to handle that we have a function to walk the dog that I will print the following message you finish walking the dog let's create a function to take out trash then we will print you take out the trash then another function of get mail as in get the mail from the mailbox then I will print you get the mail just to simulate these functions taking an indeterminate amount of time I'm going to import the time module to help us let's say walking the dog takes 8 seconds I will access the time module call the Sleep Method and pass in 8 for 8 Seconds when we call the walk dog function we'll wait around for 8 seconds then finish walking the dog this chore will take quite a bit of time to complete taking out the trash it's fairly quick taking out the trash will take 2 seconds getting the mill will take 4 seconds let's call these functions and see what happens we will Begin by walking the dog I will call the walk dog function followed by take out trash function and the get mail function here's the result we're going to wait around for 8 seconds until the walk dog function is complete right about now you finish walking the dog you take out the trash and you get the the mail these functions are running on the same thread the main thread our main Python program we have to complete these chores in order one by one because they're all running on the same thread instead of walking the dog and then when we're done taking out the trash and then when that's done we get the mail we could accomplish all three tasks at the same time let's say we have a thread object we could say thread one or to be more descriptive let's say we have chore one let me zoom in a little bit chore one is going to contain a thread we will access the threading module call The Constructor for a thread we have to pass in a keyword argument of Target what is the first chore that we have to do let's walk the dog to start this thread we will take our thread object of chore one and call the start method to start it okay let's do this with chore 2 access the threading module call the thread Constructor pass in a Target then the name of a function take out trash chore 2. start and then we have chore 3 I'll just copy what we have cuz I'm feeling lazy chore 3 will be get mail here's the result now we finish taking out the trash first then we get the mail then we finish walking the dog so we're executing these functions concurrently we're multitasking we're taking out the trash and getting the mail and walking the dog all at the same time one thing that I did want to point out notice how we finished taking the trash first followed by getting the mail then walking the dog these tasks finished in a different order compared to when we weren't multi-threading that's because taking out the trash finished first it took 2 seconds getting the mail took 4 seconds and walking the dog took the longest it took 8 seconds previously we finished walking the dog first then took out the trash then got the mail when all the chores are complete I would like to print a message I will print the following all chores are complete here's what happens currently we get this message that all chores are complete but we haven't finished any yet we're still completing them there may be a times you want your program to wait for all threads to finish before we print that confirmation message that all chores are complete we're going to use the join method method take each thread use the join method we'll do this with chore 2 and chore 3 as well with the join method we will wait for these threads to finish before continuing with the rest of the program here's the result now you take out the trash you get the mail and you finish walking the dog all chores are complete complete when constructing a thread object and we have a keyword argument of Target if some of these functions take parameters for example with the function of walk dog let's say we have a first name I will convert this print statement to an F string we will display first for the first name you finish walking whatever your dog's name is so when we're creating a thread and the target is that function and that function accepts arguments we need one more keyword argument and that is args we will send this function a tupple we need to set a parentheses within this tupple we will list our arguments let's say that our dog's first name is Scooby now since this is a tupple if we only have one argument we have to end that tupple with a comma to let python know that this is a tupple here's the result you take out the trash you get the mail you finish walking Scooby all chores are complete if we were missing this comma this is what would happen we're no longer passing in a tuple what if we have multiple parameters we have first for first name and last for last name you finish walking first and last we have first name of Scooby last name of do you take out the trash you get the mail you finish walking Scooby-Doo all chores are complete all right everybody so that is multi- threading it's used to perform multiple tasks concurrently as if we're multitasking we're executing multiple functions at at the same time multi-threading is good for Io bound tasks such as reading files or fetching data from apis and while everybody that is an introduction to multi-threading in Python