Coders Packet

Multiprocessing Vs Threading in Python

By Raagan U

This project is about the differences between threading and multiprocessing in Python. This uses the Port Scanner to demonstrate the processes.

Why Threading and Multiprocessing?

Both Threading and Multiprocessing are helpful when we are doing asynchronous tasks. Some tasks which can be executed non-sequentially can be implemented faster than the normal way. Tasks apart from the sequential way can be executed Concurrently and also in Parallel. The whole meaning of Concurrency and Parallelism is often confused. Concurrency is NOT Parallelism. There are numerous resources that explain the difference between concurrency and parallelism.

The Port_Scanner issue:

The Work of a Port_Scanner basically, is to talk to the specified IP address on the specified port and say whether it is open or not. Though it seems simple, it takes a lot of time to scan all ports of an IP. This is where threading and multi_processing come into play. The objective of port scanning is to report whether the port is open or not, but we are not restricted to a particular order. So this can be executed asynchronously.


Threading in simple words, what it does is creates threads and get them executed independently, and returns its result to the main process. Threads are small subprocess that can get executed easily and independently. 


Multiprocessing on the other hand creates multiple processes, not threads. The Parent Process (the main program) issues child processes and the child processes do their work and return the result to the parent process.

The Windows Issue:

We cannot include the multiprocessing or threaded code block on any other function than main in windows, Because the threading code_block must be safeguarded by if_main always in windows to avoid unnecessary recursive calls of subprocesses. But in Linux,  the fork() creates subprocess easily. In Linux, there is no need for the threaded code_block to be safeguarded by the main block.


When using the threading or multiprocessing module in Python, we have things to care i.e to start threads or processes and to join them and get their results. To avoid this, we use concurrent.futures of Python to create threads and subprocesses, and the remaining things are handled by themselves. We use the executor object to accomplish this in our code.

Download Complete Code


No comments yet