Multi-processing and Multi-threading

Python’s GIL problem

CPython (the standard python implementation) has something called the GIL (Global Interpreter Lock), which prevent two threads from executing simultaneously in the same program. Some people are upset by this, while others fiercely defend it. There are workarounds, however, and libraries like Numpy bypass this limitation by running external code in C.

In Python, When to use threads vs processes?

  • Processes speed up Python operations that are CPU intensive because they benefit from multiple cores and avoid the GIL.
  • Threads are best for IO tasks or tasks involving external systems because threads can combine their work more efficiently. Processes need to pickle their results to combine them which takes time.
  • Threads provide no benefit in Python for CPU intensive tasks because of the GIL.

*For certain operations like Dot Product, Numpy works around Python’s GIL and executes code in parallel.

From Experioence:

Multi-Processing cannot share properties in classes.

 


Reference: Intro to Threads and Processes in Python