When programmers run a simple program of Python, execution starts at the first line and proceeds line-by-line. Also, functions and loops may be the reason for program execution to jump, but it is relatively easy to see its working procedures and which line will be next executed. Programmers can put their fingers and can trace the lines of codes that will be executed; this is called single-threaded programming.



However, in the case of multi-threaded programming, it's like putting a second finger on your program. Both the fingers move the same way and will be executed simultaneously.

What are Threads?

It is the execution of a tiny sequence of program instruction that can be managed independently and is a distinctive part of the operating system. Modern OS manages multiple programs using a time-sharing technique. In Python, there are two different kinds of thread. These are:

  • Kernel Threads
  • User-space threads or User threads

Why Use Thread

Thread plays a significant role in application programming. All the GUI programs and web servers are threaded together. The main reasons for using threads are:

  • Parallel Computation: If any user has a multiprocessor machine, then the thread can allow doing parallel processing with the goal of an increase in processing speed.
  • Standardization: It became a standard approach for all programming languages as it increases programming speed.
  • Parallel I/O (Input/Output): When we talk about the speed of input & output, it is comparatively slow in CPU. By placing each i/o operations in multiple individual threads, programmers can make use of operations done in parallel with each other & with the computation speed.
  • Asynchronous Events: Multiple threaded applications can deal with asynchronous actions. For example, in a program, programmers may not know whether the next action will be to use the mouse or to use the keyboard. By planting a separate thread for each action, i.e., two threads both for mouse and keyboard, programmers are able to code a cleaner, efficient application, which is to use non-blocking I/O operations.

Thread Modules in Python

There are two ways of accessing Python threads. These are by using:

  • py module
  • py module

It is to be noted that the 'tread' module has been considered as of lesser use, and hence users get to use the 'threading' module instead. Another thing has to keep in mind that the module 'thread' treats the thread as a function, whereas the 'threading' is implemented as an object.

Benefits of Threading

  • For a single process, multiple threads can be used to process and share the same data-space and can communicate with each other by sharing information.
  • They use lesser memory overhead, and hence they are called lightweight processes.
  • A program can remain responsive to input when threads are used.
  • Threads can share and process the global variable's memory.

In a thread, there are three different parts. It has the beginning, an execution part, and a conclusion. It also has an instruction pointer that points to where the thread or process is currently running, and hence the thread can run several different program blocks concurrently.

Using a New Thread

It is achievable to execute functions in a separate thread using a module Thread. For doing this, programmers can use the function - thread.start_new_thread().

Syntax:

thread.start_new_thread(function, args[, kwargs])

Here, the first part is a method as told before & this method is a faster and more efficient way to create new threads. As the child thread starts, the function passes a list of args. The thread gets terminated when the function returns a value. The 'args' in the above syntax is a tuple of arguments.

Program of Threading Using Python

Example:

import threading

def coder(number):
    print ('Coders:  %s' , %number)
    return

threads = []
for k in range(5):
    t = threading.Thread(target=coder, args=(k,))
    threads.append(t)
    t.start()

Output:

Coders: 0

Coders: 1

Coders: 2

Coders: 3

Coders: 4

Methods of Thread Class

The threading module, as described earlier, has a Thread class that is used for implementing threads, and that class also contains some predefined methods used by programmers in multi-threaded programming. These are:

  • run(): It acts as the entry of the thread
  • start(): is used for starting the thread by calling the run()
  • isAlive(): is used to verify whether the still executing or not
  • getName(): is used for returning the name of a thread
  • setName(): is used to set the name of the thread


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram