What is difference between multi-threading and multiprocessing?
Multi-threading vs Multiprocessing :
- The threading module uses threads, the multiprocessing 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.
- Multiprocessing :
- Pross :
- Separate memory space
- Takes advantage of multiple CPUs & cores
- Avoids GIL limitations for cPython
- Child processes are interruptible/killable
- Cons :
- Inter-process communication (IPC) a little more complicated with more overhead.
- Larger memory footprint.
- Multi-threading :
- Pross :
- Lightweight - low memory footprint.
- Shared memory - makes access to state from another context easier.
- Allows you to easily make responsive UIs.
- cPython C extension modules that properly release the GIL will run in parallel.
- Great option for I/O-bound applications.
- Cons :
- Not interruptible / killable.
- Code is usually harder to understand and to get right - the potential for race conditions increases dramatically
- It will not resume the task if server restarts for any reason.
Comments
Post a Comment