How does the chapter explain the difference between concurrency and parallelism?
The chapter explains that concurrency does not mean full parallelism. In a single CPU, concurrency does not involve multiple processes executing in the same nanosecond; instead, it mostly avoids busy waiting by letting the CPU do useful work while waiting for slow responses. Parallelism, by contrast, implies actual simultaneous processing, which is not what concurrency means on one CPU.
On page 26, the author clarifies that concurrency is not the same as parallelism. While parallelism would involve multiple processes occurring at the same instant on a single CPU, concurrency does not require that. Rather, concurrency is mainly about avoiding busy waiting, or idling the CPU while waiting for a response. Normal synchronous Python code does one thing at a time in the order written, but asynchronous code can do a little of one task, then a little of another, and keep the CPU engaged. The distinction is that concurrency interleaves tasks to make efficient use of time during input/output waits, rather than actually running them simultaneously on the same CPU.
Key points
- Concurrency and parallelism are distinct; concurrency is not full parallelism on a single CPU.
- Concurrency avoids busy waiting by not holding up the CPU while waiting for slow responses.
- Parallelism would mean multiple processing happening in the same nanosecond, which a single CPU does not do.
- Normal Python execution is synchronous, doing one thing at a time; asynchronous execution enables concurrency by switching among tasks.
Related questions
FastAPI: Modern Python Web Development
Bill Lubanovic;
First Edition · O'Reilly Media, Inc.