Module 16: Concurrency

Threads, processes and asyncio: doing many things at once, and when each one helps.

  1. Threads: Threads overlap waiting (I/O), not calculating (GIL); protect shared data with with lock:, pass work with queue.Queue, take locks in one fixed order.
  2. Processes: ProcessPoolExecutor/Pool run CPU-bound work on every core (each process has its own GIL); guard with if name == "main":, use top-level functions, and return results since memory is not shared.
  3. asyncio: async def makes a coroutine, await pauses it so others run; asyncio.gather runs many together (results in argument order); use waitfor for timeouts, a Semaphore for limits, and never block the loop.

Ends with a 12-question checkpoint (80% passes) and a mini project: Canteen Counter Pro: the async order notifier.