In software programming, a thread is the smallest unit of execution with the independent set of instructions. It is a part of the process and operates in the same context sharing program’s runnable resources like memory. A thread has a starting point, an execution sequence, and a result. It has an instruction pointer that holds the current state of the thread and controls what executes next in what order
Python Multithreading – Pros:
- Multithreading can significantly improve the speed of computation on multiprocessor or multi-core systems because each processor or core handles a separate thread concurrently.
- Multithreading allows a program to remain responsive while one thread waits for input and another runs a GUI at the same time. This statement holds true for both multiprocessor or single processor systems.
- All the threads of a process have access to its global variables. If a global variable changes in one thread, it is visible to other threads as well. A thread can also have its own local variables.
Python Multithreading – Cons:
- On a single processor system, multithreading wouldn’t impact the speed of computation. In fact, the system’s performance may downgrade due to the overhead of managing threads.
- Synchronization is required to avoid mutual exclusion while accessing shared resources of the process. It directly leads to more memory and CPU utilization.
- Multithreading increases the complexity of the program thus also making it difficult to debug.
- It raises the possibility of potential deadlocks.
- It may cause starvation when a thread doesn’t get regular access to shared resources. It would then fail to resume its work.
How To Use The Threading Module To Create Threads
The latest <threading> module provides rich features and greater support for threads than the legacy <thread> module discussed in the previous section. The <threading> module is an excellent example of Python Multithreading.