Python Programming from First Principles: Learn to Code, Think Computationally, and Solve Real-World Problems
PDFEPUB
BOOK 000 · CHAPTER 8 · Concurrency & Parallelism

Concurrency & Parallelism

Modern software rarely does just one thing at a time. A data-science pipeline might download files, clean datasets, and update a database. A web server might handle hundreds of users simultaneously. A desktop application might respond to clicks while performing background computations.

To keep programs responsive and efficient, Python gives us tools for running multiple tasks at once — a world known as concurrency and parallelism. - Concurrency is about managing many tasks so they appear to run simultaneously. Only one task may run at a time, but the program switches quickly between them whenever one must wait (for example, on network or file I/O). - Parallelism, in contrast, is about running tasks at the same moment on different CPU cores.

Python provides three major approaches to handle this multitasking world:

  • Threads — ideal for I/O-bound tasks that spend time waiting.
  • Processes — ideal for CPU-bound tasks that need real parallel performance.
  • Asyncio — ideal for running many lightweight asynchronous operations inside a single thread.

Before diving into these tools, we must understand the key architectural piece that shapes Python’s multitasking behavior: the GIL.