Python Multiprocess and Threading Differences

Question: I am trying to understand the advantages of multiprocessing over threading. I know that multiprocessing gets around the Global Interpreter Lock, but what other advantages are there, and can threading not do the same thing? Answer: The threading module uses threads, the multiprocessing module uses processes. The difference is that threads run in the same memory space, while processes have separate memory. This makes it a bit harder to share objects between processes with multiprocessing. Since threads use the same memory, precautions have to be taken or two threads will write to the same memory at the same time. This is what the Global Interpreter Lock is for.

Spawning processes is a bit slower than spawning threads.

Once they are running, there is not much difference.

Tip1: The GIL(Global Interpreter Lock) in cPython does not protect your program state. It protects the interpreter's state.

Tip2: Also, the OS handles process scheduling. The threading library handles thread scheduling. And, threads share I/O scheduling -- which can be a bottleneck. Processes have independent I/O scheduling.

Last updated