Hey everybody welcome to the last video of us actually going over some code in Python.
Now what I want to talk about in this last video is something called threading.
Now I know you've mentioned this before where we talk about processes that can fork off a child of themselves.
Now this is where we're actually going to implement that in the code itself.
So Python has a default module called Red or threading or we're going to use a sub process of that or
a sub module from that called thread or a subclass from that.
So we do from Redding which is the main module we're going to import thread which is what we need to
read and we're going to import time because we're going to need that in a minute.
Perfect.
So this is what we're going to be using when dealing with threading in python.
Now there's a few implementations and other modules that you can use to make this a little bit easier.
But when we're handling with we're all threads this is how we're going to do it now to thread something
it can be threading a class or it could be threading a particular function.
It could be threading you can pretty much thread anything you want as long as it's some sort of chunk
of code that you can have perform some sort of operation.
So to do this first thing we need to do is we're going to create another function above Main.
So we're going to call this run right and inside of this we're going to pass in a thread.
Just for sake of us being able to work with a digit that you can visibly see on the screen right.
So what we're gonna do is we're gonna print the thread I.D.
in weeding
waiting state
so when we're afraid predators first hits this thread we're going to print this out to show that this
is it's now sitting in a waiting state.
And the reason it's in a waiting state is we're going to do timed out sleep to sleep.
The current thread for only so many seconds so we're going to say five seconds.
Right.
And then once that's done we're going to want to print this again 3rd I.D.
idea 0
0 0 in finished state.
So now we've completed so pretend like this is the function that we want to run some sort of threading
on.
Now those can be running a port scan this can be running some sort of commands on a system.
It could be anything you want it to be it could just be as simple as just printing something out and
then waiting a few seconds and printing something out again.
So what we're gonna want to do is the way you start a thread is you create a thread object with a parameter
of target being whatever the function name is and then you pass and arguments.
So what we're gonna do is we want to create five threads right.
So what we're gonna do is we're gonna do this in a for loop.
So for i in range of 5 so we're going to run this five times.
This should not be anything new for you guys.
Now we're going to want to create D a thread variable which is just anything you want to name it and
then we're gonna use the thread object.
Now here's where we talked about those parameters we set our target equal to run.
Now we're not using the parentheses here which is important.
This is just the name of the function that we want to use.
So in this case.
Right.
And then args we're going to set and it takes it as a couple because they cannot be changed.
Now what's important understand here is you always need at least you can't have a couple of just like
I write.
You have to put a comma at the end.
Just note that's a bit of a nuance but if you put in if you don't have anything you can just do that.
That's fine.
But if you put a variable and you need it and you need a comma.
So in this case we're passing an I guess we're going to get the thread 80 to this number so that'll
be 0 1 2 3 and 4.
Right now this doesn't actually do anything.
So if we just ran this right now it just does nothing.
It creates the thread objects but that's it.
Now you need to actually start each thread so thread that start.
So this will start each thread and then it will kick off and allow the application to go through.
So what we're going to do here is we're going to print
made it to the end.
So we'll go ahead and run this and we'll see what the results are.
So as you can see here give it five seconds perfect so you can see here zero one two three and four
waiting states they've all hit that and then it said made it.
So it made it past this code and it started executing more because we have our main thread which is
the one we're running with this one we ran Python made up high.
It actually created it the first parent thread then as it spawned off each of these.
These are in separate threads and then we made it because it finished creating and starting the threads
in the loop and then it continued on like normal normal code execution.
That's why we see made it here.
However because these threads were still in a sleep mode the final code may have ran and completed in
that thread is just sitting in a way the parent thread sits in a waiting state until all children are
finished such as waits there's no extra code being ran here and if there was it would be then running
that code instead.
And then we have zero one two three and four completing as we can see there in their finished state
which means those threads of close and only as you can see here it's gonna to wait that five second
so only until those threads have closed while the parent threads stop which is what we see happening
here.
Now this is a very basic example but it helps you get a better idea of how to work with threads in the
long run when you have an application that might need to be sped up such as you're running a you're
making a custom port scanner that looks for a particular issue on hosts you've got your it's going to
take a while to get through an entire subnet of a network.
So in this case if you can have 20 different threads all smacking different parts of the network you
can get things done much more rapidly than you would a single process making a connection because each
connection could take four or five seconds depending on what you're trying to do.
So go ahead and play around with this.
Try to combine a few things together.
But in our next hour you're gonna be going over a hands on project that I want you guys to build which
will include threading as a possible solution.