Multithreading versus multiprocessing
Now that we have come to the end of our discussion on multi-processing, it is a good time to compare and contrast the scenarios where one needs to choose between scaling using threads in a single process or using multiple processes in Python.
Here are some guidelines.
Use multithreading in the following cases:
The program needs to maintain a lot of shared states, especially mutable ones. A lot of the standard data structures in Python, such as lists, dictionaries, and others, are thread-safe, so it costs much less to maintain a mutable shared state using threads than via processes.
The program needs to keep a low memory foot-print.
The program spends a lot of time doing I/O. Since the GIL is released by threads doing I/O, it doesn't affect the time taken by the threads to perform I/O.
The program doesn't have a lot of data parallel operations which it can scale across multiple processes
Use multiprocessing in these scenarios:
The program performs a lot of CPU...