Getting started with “thread” in Python(Part-2)

Preet Padariya
5 min readApr 12, 2022

--

Introduction

As we have discussed in part-I, that what is thread and why we need thread? As you might be aware that threading is a term which usually needed to achieve multi-tasking. In this part-II we will be giving you some glimpse of threading implementation in python. With the basic thread terminologies that we have covered in part-I will help you to understand better in this part so, let us start first look at “Threading” module in python.

Threading module in python

This threading module was included in python 2.4 which has provided much more powerful and high-level support for threads than the previous once.

In addition to some advance methods, this threading module consist “thread” class that implements threading and the methods; which are provided by the “thread” class are mentioned below.

1. run()

2. join([time])

3. start()

4. getName()

5. setName()

6. isAlive()

Creating a thread in python

So, let’s take a look that actually, how we can create the thread in python?

There are basically two ways to do so; first is by using Thread class and second is by using Thread class object. Both are mentioned briefly below.

By inheriting thread class

So here, we imported thread class from Threading module. After that, In Test class we have inherited in Thread class and overridden the run method. In run method we are calling “getName” method which returns the name of current thread which is executing.

CODE

After defining class, we created two objects of Test class. Now, if we want to start thread then we have to call start method. If we directly call the run() method then it will not create a Thread. After invoking start() method in this program we have total three threads. One is main thread second is t1 objects thread and last is t2 objects thread.

OUTPUT

What is main thread?

In normal condition, the main thread is the thread from which the python interpreter was started. It is a basic and compulsory unit of execution for any program because without thread there is no existence of process. You can also consider that it is a by default thread, which will run without any invocation for execution of any python file.

You can also consider as abstract thread from which any number of child threads can be spawned, as we are doing.

By Using Thread class object

If we don’t want to create our own class then we can directly create thread class object and use it. At the time of creating object of thread class, we can give name of the thread and also target method name which will be called when we start the thread. If we don’t want to give name and other parameters then it’s also fine.

Here we have defined a two methods method1() and method2() and in that we are printing the name of the thread from where it is called.

After that we created t1 and t2 object with name Thread_1 and Thread_2 respectively. And also passing target parameter for t1 and t2 that is method1 and method2. Here, target parameter means when we called start() method then it is internally called our passing method.

And in Third object we didn’t give any parameter.

CODE

The output look like below.

OUTPUT

You might be wondering that what are these methods; which is used in here. Such as, start(), run(), etc. So, for that let’s take a look at those such methods.

Useful methods in thread class

There are several kinds of methos are there in thread class. Let’s talk about each methods, which are there exist in thread class.

Run method

The run method is inbuilt empty method of Thread class. The run() method defines the body of the thread. The run method gets it code the following two way.

1. when the run method is overridden in a subclass, as we have done in first example.

2. Second is using Thread class constructor. When Thread class object created a callable object is passed as a target through the constructor. As we have done in second example.

Start method

The start method is an inbuilt method of Thread class. Which is used to start a Thread. The start method calls run method internally.

This method must be called at most one time for one thread. If it called more than once it will give Runtime error. The return type of start method is none.

Join method

When join method is invoked, the calling thread is blocked till the thread object on which it was called is terminated.

For example, if join method is called from a main thread, the main thread waits till the child thread on which join invoked exits. The use of join method is, if join method is not invoked then the main thread may exit before the child thread, which will result undetermined behavior of programs and affect program invariants and integrity of the data on which the program operates.

Python code

CODE

Here, this program creates two threads one is odd_thread and second is even_thread. odd_thread makes lists of odd numbers and even_threads make lists of even threads. Also, there is two functions print_odd() and print_even() which makes list of odd and even numbers and prints it.

The output look like below.

OUTPUT

Python multithreading Applications

Most of the application you use in daily life have multiple threads running behind the scenes. Consider your internet browser. It is best example of multithreading programming. At any time, you may have number of tabs open, each one displaying various types of websites, video, blog, etc.

Another best example is Microsoft-word, Games, etc.

GitHub repositories

HENIL MISTRY

HARSHIL PADASALA

Prepared by(MEDUIM PROFILES)

HENIL MISTRY

HARSHIL PADASALA

PREET PADARIYA

--

--