Overview
Both multiprocessing and multithreading both are ways to achieve multitasking
Multithreading
- will share the same address space (global variable can be access)
|
|
real example of making async calls
|
|
Multiprocessing
Multiprocessing via Queue
|
|
Lock
Process or Thread 1 -> Shared Resource (Memory, Files, Databases etc.)
Process or Thread 2 -> Shared Resource (Memory, Files, Databases etc.)
Pool
|
|
Multiprocessing Queue v.s. Queue Module
- different processes use different memory space
- heavy
- the benefit of multiprocessing is that error or memory leak in one process won’t hurt execution of another process
Multiprocessing Queue
|
|
- Lives in shared memory
- Used to share data between processes
Queue Module
|
|
- Lives in in-process memory
- Used to share data between threads
Questions
- How to know when all threads are done with the tasks?
Global counter with initial value of the total number of tasks, and threads update the global counter with a global lock. Once the global count turns 0, the thread that gets the lock and counter report back to the main process that all the work is done. References
- https://www.youtube.com/playlist?list=PLeo1K3hjS3uub3PRhdoCTY8BxMKSW7RjN