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 fairly 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 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 operating system. Modern OS manage 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 Threads?
Thread plays a major 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 multiprocessor machine then the thread can allow doing parallel processing with the goal of 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 don't 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 can able to code a cleaner, efficient application which is to use non-blocking I/O operations.
Threads 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 '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 like 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 is able to 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(). 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.
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