Programming Python

Multithreading vs Multiprocessing vs Asynchronous

Written by devendermahto

Threading
Pros

  • 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 / network-bound applications

Cons

  • cPython – subject to the GIL
  • Not interruptible/killable
  • If not following a command queue/message pump model (using the Queue module),
  • then manual use of synchronization primitives become a necessity (decisions are needed for the granularity of locking)
  • Code is usually harder to understand and to get right – the potential for race conditions increases dramatically

https://pastebin.com/iHM3rF1u

Multiprocessing
Pros

  • Separate memory space
  • Code is usually straightforward
  • Takes advantage of multiple CPUs & cores
  • Avoids GIL limitations for cPython
  • Eliminates most needs for synchronization primitives unless if you use shared memory (instead, it’s more of a communication model for IPC)
  • Child processes are interruptible/killable
  • Python multiprocessing module includes useful abstractions with an interface much like threading.Thread
  • A must with cPython for CPU-bound processing

Cons

  • IPC a little more complicated with more overhead (communication model vs. shared memory/objects)
  • Larger memory footprint

https://pastebin.com/1dLdGFmA

Async

waits for a thread to be ready before switching to it, I am still working out if this is actually any good for things

https://pastebin.com/2j8KEYwa

About the author

devendermahto

Leave a Comment