Hello guys.
So we are going to continue the discussion with respect to multithreading and multiprocessing.
In this video we are just going to discuss about multithreading.
We will be seeing some very good examples like why multithreading will be useful.
And we will also do some practical implementation with respect to that with the help of Python.
Okay.
So let us go ahead.
And for that I will again hide my face so that your focus is completely on the screen.
Okay.
Now uh, for performing multithreading okay.
Let me go ahead and write this.
So this is nothing but a multi threading.
First of all we need to understand when to use multi threading.
This is a very important question when to use multi threading.
Right.
And for this there are two important reasons.
One is I o bound task okay.
Now what is this I o bound task.
Task that spends more time waiting for I o operation file operation or network request.
So in this case you can specifically use multi-threading wherever you have where you are waiting more
for the I o kind of operation.
And the second reason that you have when when you should basically use multithreading is when you have
concurrent execution, when you want to improve the throughput of your application by performing multiple
operation concurrently.
Okay.
So let me just go ahead and show you one example.
So for implementing multithreading we will go ahead and import threading.
Okay.
So this is a kind of library that is package that is used in Python okay.
We'll also go ahead and import time okay.
Now let me just go ahead and create one function okay.
So here I will say hey definition print underscore numbers okay.
And this is my function.
And let me just go ahead and write for I in range Of five.
Okay.
For I in range of five.
And here I'm just going to put semicolon or colon.
And let me just go ahead and write print f number okay colon I okay.
So this is what I am actually printing okay.
Along with this.
Let me just go ahead and create another definition where I'm going to print my letters, what letters
I am giving over here.
And let's say I will go ahead and write for letter in uh, text that is a, b, c, d, e in my string
a, b, c, d e.
And here we are just going to go ahead and print my letters by using an F string.
And here I'm going to say hey use letter colon.
And this will basically be my letter okay.
So I'm printing the numbers.
This function is basically printing the numbers.
And this function is printing the letters.
Now we are just going to go ahead and that create or let me just go ahead and execute this entire function
itself.
Okay.
So what I will say, hey, let's go ahead and execute this.
So I'm just going to go ahead and write print numbers.
And then this should execute print letter okay.
Now here what I'm actually going to do I'll just go ahead and execute this.
So in order to execute it I'm right now in my Streamlit folder I have to go inside my multi-threading
folder.
So let me just go ahead and write CD dot dot and let me go ahead and write CD 16 multithreading and
multiprocessing I'll clear the screen.
Now I'm just going to write Python multithreading.
And for this also what I can actually do is that, um, let me uh, start the time.
Time also over here.
Okay.
So, uh, in order to start the time, what I can do is that I can just go ahead and write t is equal
to time dot now.
Okay.
And here I will say my finished Time finished.
Underscore.
Time is equal to.
Um.
Time dot.
Now.
Minus minus t.
Okay, so I'm just trying to basically get the time and how much time it has probably taken.
And here I'm just going to print my finished time.
Okay.
The time to probably call this particular function.
And it is very much simple.
I have just used time dot now and here I'm saying hey time dot norm minus t which will be this particular
time.
That is the time when we started executing this particular function.
Now quickly I will just go ahead and write Python multithreading dot pi and let's go ahead and execute
it.
So here it shows time has no attribute.
Okay.
So I'll go ahead and write time dot time dot now.
Okay.
And here also I should basically write time dot time dot now.
Okay.
So I missed that because inside that time there will be another library which is called as time.
Now let's go ahead and execute it again.
I'm getting an error.
So here you can see that it is saying it has no attribute now.
Okay so what I will do instead of using this now I will just go ahead and write time dot time and let
me also remove this and just go ahead and use time dot time.
I think with the help of this also it will get executed again.
I'm not going to edit this entire, uh, video because I want you all to see the errors and you may
also face this kind of errors.
Now I'm just going to open this.
And now let's go ahead and run it.
So here you can see uh, I'm getting this number 01234.
I don't know why the letters has not been printed.
So I had not called this function.
Okay.
So I will just go ahead and execute this again and let's execute it okay.
So I'm actually getting number 01234.
And then letter a b c, d and all.
This particular time is basically over here.
This is fine.
Let me do one thing okay.
Let me use uh a concept in threading where I will uh, not in threading, but I will just make sure
that I will tell to probably when I'm printing each and everything, I'll say, hey, use this time
and make this execution sleep for two seconds, okay?
And similarly here also I will say whenever we are calling this, we will make this sleep for two seconds.
Okay.
Now let's go ahead and execute it.
Okay.
And here you'll be able to see that I'm executing Python multithreading dot p y.
Now see see see the things okay.
So number zero has come.
It has slept for two seconds.
Then it went to number one two okay three four okay then letter a b.
So we have made this this entire thread to sleep for some seconds okay.
For two seconds.
The total time it basically took somewhere around 20s.
Okay.
Now the reason I made it sleep over here.
Let's say that there is an I o operation that is basically happening over here, and that is the reason
I just made it sleep.
In order to just write I o operation I.
Operation can be reading a file or accessing the file.
Anything it can be.
Okay.
Now here what I have done is that I have just made this to sleep.
When this is actually sleeping, we are just waiting for this particular thread to become active or
complete that particular eye operation and come back to this particular code, right?
What if I say that when my thread is basically sleeping, or when my execution is, uh, when this particular
code is getting executed over here, I should allow this code to get executed, right?
Because this code is already there.
And for this also, we are waiting for the execution because we are calling this particular function
first and then this function first.
So if anything sleeps over here in this particular function I should allow this function to continue.
Right.
And right now a single thread is running right.
A single thread or a single thread over here is specifically running line by line.
It is running right.
What I can do is that I can create multiple threads.
One thread will be responsible in running this entire code and the other thread will be responsible
in running this particular code.
And that way I don't even have to wait for every function to get executed.
So this will be executing concurrently like Parallely.
Okay, so let me just go ahead and show you that thing how we can actually do it.
So I have imported threading right now.
I will just go ahead and instead of just directly calling this particular function, first of all I
will go ahead and create two threads.
So let me just go ahead and create two threads.
Right now I just have one.
One is getting executed line by line.
So first thread is nothing but t one.
And here I will be saying, hey, call this thread and create a new thread over here and inside this
thread use the target function.
Okay, which is nothing but my print underscore numbers.
Okay, so this is my target function with respect to this particular thread.
So t one will be responsible in executing this particular code okay then similarly t two I'll be going
and using it.
So I'll be writing threading dot thread.
And here my target is going to just be my print letters.
So here we are just going to go ahead and write print letter okay.
So this is my T1T2 thread okay.
Now I will just go ahead and start my thread okay.
Now before starting my thread I will just go ahead and start this timer.
Now instead of directly calling this numbers.
Okay I will just go ahead and write.
Hey, let's start the thread.
So start the thread.
In order to start the thread I just have to say t one dot start okay?
And then you can see I will go ahead right t two dot start okay.
So this is basically starting when this is starting that basically means this will get executed.
And when the sleep when when when this particular line of code is getting executed you'll be seeing
that my thread, my second thread will start getting executed.
Okay.
Since this uh, over here, you can see that one of the thread will sleep, but the other thread will
concurrently getting executed.
Okay, it will get will execute.
Okay.
Now, finally, uh, once I finish this particular time, uh, here.
What I'm actually going to do quickly.
I will also wait for the threads to complete.
Now, in order to wait for the entire threads to complete, I can go ahead and write T1 dot join.
Okay.
And finally, once the execution basically happens, then both this particular threads will be joined
back to the main thread.
Okay.
There will be one main thread that will be working.
Okay.
And then we will go ahead and see the time.
Now let's go ahead and run this.
And now you'll be able to see one amazing thing that will be happening.
Okay.
So I will go ahead and execute this code.
Now see the magic okay.
So letter A number zero.
Okay.
Number one.
Letter B okay.
Number two.
Letter C number.
Letter d.
Number three.
Letter four.
Number four letter e.
And then now the total number of time like total amount of time that is basically taking is half.
Right.
Because just we have used two threads over here.
If I just increase the number of threads during the sleep time.
Right.
Let's consider that as an I o eye operation.
But parallelly or concurrently we have created one more thread and that is also taking care of the execution
of the other function.
If I have three four functions, I can actually do it right.
Now this is one beautiful example of multithreading where I'm showing you right when when some when
one of the thread is busy, then we will make sure that we'll try to execute the other function with
the next thread itself.
Right.
And here the simple reason is that first of all, you need to know how to create a thread, how to start
that thread, how to wait for the thread to complete just by using this particular operation that is
dot join.
And finally, you can see with respect to the finish time and here you can see the difference right?
So uh, I hope you are able to understand this particular problem statement and you are able to understand
about multi threading.
We will see more examples as we go ahead.
But before that let's go ahead and see the next topic which is with respect to multiprocessing.
Then we will see more examples as we go ahead.
So yes this was it from my side.
I hope you like this particular video.
I'll see you in the next video.
Thank you.