Multithreading versus multiprocessing
Now that we have come to the end of our discussion on multiprocessing, 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 ...