Hey guys,
welcome to Free Tutorial TV and Python tutorial for beginner .
in the last video I have given
you a brief introduction about threads and threads in fight and in this video we will discuss about
a module called Try to model.
Now if you remember in the last video I have told you that threads module is considered as deprecated
in python 3 but if you want to use threads module you'll in Python 3 we can still use Thread you're
using our module called underscore to threads module.
And this is for backward compatibility.
So I just wanted to give you a brief demo about threads model in Python 3 so you will have some threads module in Python 3.
So let's get started.
So first of all in order to use threads module you'll need to import that and then generally you will the
right thread.
But in order to use the train module in Python 3 you need to use the underscore thread here.
OK so threads module is not directly available but it's available using this underscore threads module and
for printing and giving some delay to our functions we are going to import a module all the time.
Now using threads module it's possible to use functions in separate threads.
So we are going to create a function and then we are going to execute this function in separate threads.
So let's define our functions.
And I'm going to name my function as print underscore epoch and if you don't know what is a epoch
ipoch is the start of January first 1970 and we are going to try to print the number of seconds from
a epoch till now.
And inside this function we are going to provide two arguments.
First is the name of your thread.
So I'm going to just write name of thread and the second argument.
We are going to give is the delay.
OK so this is your custom function.
You can provide any name here and any number of arguments here.
And now inside this function first of all I'm going to create counter.
So I'm going to just write the count and I'm going to initialize this count with zero.
Now inside this function what I'm going to do is I'm going to loop around and inside the loop I'm going
to provide some delay.
So we will be able to see the trade execution.
So let me just provide the while loop here and we are going to just check whether the count is less
than 3.
And inside this value we are going to sleep for some time using that time model.
So I'm going to just write time dot sleep and we are going to just pass this delay argument inside
the sleep function . so I'm going to just try the delay here which will be that delay in seconds.
So for example if I provide the delay all for one . all or while you will sleep for one second now
after this we are going to increment counter.
So I'm going to just write count and then the last equals one of which is going been tremendous value
of Kondo by one.
Now after this in order to see whether our thread is working or not we are going to print something.
So let's print something using a print function.
So what we want to print.
So let's say we want to print the name of the thread whatever name we will provide for the thread and
then I'm going to just give some the data here let's say this is some line and then we are going to
print that time since epoch.
So in order to print time since epoch we will use this time module and then we just need to use the
time function inside that time or Noul.
So using this print function we will know which thread is executed and at what time it's executed.
So now we have a function.
And now this function we want to execute inside that thread.
So in order to execute this function inside the threads.
First of all I'm going to use this threads model and that is a special function inside this threads model
we just called us DOD new thread function.
So I'm going to use that function.
So let's just call this function which is Todd underscore new underscore threads.
Now if you hover over this function and when I press control on Windows or command on Mac and then I
hover over this function you will be able to see the signature for this function.
So this function takes three arguments flossed argument is the function which we want to execute inside
the threads.
**************************************************************************
please like and follow us in Facebook and Instagram.
facebook: / technologytv
Instagram: / heikhamsingh
Twitter: / hanandkumarsing