Tug of War: MultiProcessing Vs MultiThreading

Nouer Uz Zaman
2 min readDec 31, 2021

--

Within Python one of the most confusion concept to get your head around is acknowledging the difference between Multi-Processing and Multi-Threading.

šŸ‘€šŸ‘ After reading a lot of materials eventually I managed to comprehend the difference. I will attempt to clarify it with most basic explanation.

First of all we need to understand what is a process. A process is like a program that has been loaded into memory of your system whenever you attempt to do something. A thread is the unit of execution within a process. Thus, a process can have multiple threads running through and each thread uses the processā€™s memory space therefore all threads shares the same memory. However, each Process has itā€™s own separate memory.

A code that aims to use multiple processes uses the module Multiprocessing in Python. A code that aims to use multiple threads uses the module Multithreading. Quite Initiative, no(Cā€™est assez dā€™initiative, non)!

As shown in the diagram above, multiprocessing has a trait of parallelism which means all process run simultaneously together. Whereas, multithreading has a trait of concurrency which means one thread runs one at a time.

Best use

1) Multiprocessing is best used for CPU-heavy tasks. For instance, if you want to run a Python code on the system but the code is too complicated and long. Then Multiprocessing is used to separate each task which will make the overall code more efficient and fast.

2) Multithreading is best used for IO (Input/Output) based tasks like querying a database or loading a webpage. Here the CPU is just doing nothing but is waiting for an answer. Biggest point to note is that because threads use the same memory space it is safe to use ā€œJOINā€ commands to join them together or else the main(final) thread could run before the proceeding threads which can destroy the code.

Conclusion

Multiprocessing is best used for CPU based jobs. If you running a code it is taking away lot of your CPU storage then use MULTIPROCESSING. If you want to query a database like SQL e.g. run sql commands on many databases then use MULTITREADING.

For now I have kept it very short because I aim to write short articles to absorb the information well. I will post more articles on this topic in the near future.

Au revoir!!

--

--