Introduction to Multi Threading vs Multi Processing (Part 1)

Jonathan Tan
2 min readOct 18, 2023

--

Concurrency and parallelism is a fundamental aspect of modern software development, enabling programs to perform multiple tasks simultaneously, improving responsiveness and performance.

Two popular approaches to achieving concurrency are multithreading and multiprocessing. Each approach has its own strengths and best use cases.

Multi-Threading

Multithreading involves the use of multiple threads within a single process, making it ideal for I/O-bound (Input/Output-bound) tasks that involve waiting for external resources, such as reading from files or making network requests. Python’s ‘threading’ module allows you to create and manage threads, making it relatively straightforward to work with.

Multi-Processing

On the other hand, multiprocessing leverages multiple processes, each with its own Python interpreter and memory space. This approach is excellent for CPU-bound tasks where you want to fully utilise multiple CPU cores for parallel processing. The ‘multiprocessing’ module provides a powerful tool for creating and managing processes.

When to use what?

When and Why to use What?

When confronted with a scenario where the CPU is constrained or a substantial amount of CPU-intensive work needs to be performed, multi-processing stands out as a viable choice. In multi-processing, multiple independent processes run in parallel, each with its own memory space and Python interpreter. This approach effectively leverages the full potential of multi-core CPUs, as each process can be assigned to a separate core. The result is a significant boost in overall computation power, making it ideal for CPU-bound tasks.

On the other hand, multi-threading excels when dealing with I/O-bound tasks, where the program often awaits input or output operations, such as file I/O, network communication, or user interactions. The key advantage of multi-threading lies in its ability to maintain responsiveness during I/O operations. While one thread is blocked, waiting for the I/O operation to complete, other threads can continue to perform other tasks. This concurrency allows for more efficient resource utilisation and keeps the system responsive, preventing it from becoming sluggish during periods of I/O waiting.

Since threading allows for data to be accessed while running, it is important for the data to be protected with Lock() or mutex (mutual exclusion).

In conclusion, the choice between multi-threading and multi-processing depends on the specific requirements of your application. Multi-processing shines in CPU-bound scenarios, fully utilizing available CPU cores, while multi-threading is the preferred approach for I/O-bound tasks, ensuring responsiveness and efficient resource management during I/O operations.

A useful video to help visualise how Multi-Threading vs Multi-Processing works in Python.

--

--

Jonathan Tan

👋 I'm Jonathan, a software engineer from Singapore. I am passionate about learning and crafting new solutions. Feel free to connect with me!