liveBook · Manning

Concurrency

As explained in The Coder Cafe

Concurrency is an execution model that structures tasks so they can be managed independently without requiring them to run simultaneously.

Concurrency structures tasks so they can be managed independently, even when they do not run at the same time. Parallelism is different: it executes multiple tasks simultaneously using additional resources. The distinction matters when software must stay responsive while handling work that takes time. section 2.1 section 2.0

A coffee shop with two queues

Adding a waiter and a coffee machine lets two orders be served at once: parallelism. Separating order-taking from coffee-making, with a queue between them, changes the structure of the work: concurrency. An order can wait for coffee without stopping the next order from being taken. The two roles must still pass order information between them. section 2.1.1

Independent tasks create choices—and coordination problems:

  • Coroutines pause and resume tasks within one thread. They provide concurrency without requiring parallel execution and are useful when work spends time waiting on I/O. section 2.2
  • When threads share a resource, a mutex restricts access to one thread at a time; a semaphore permits access up to a set limit. section 2.5
  • Avoid concurrency when lightweight tasks or frequent synchronization make its overhead greater than its benefit. Preventing a data race also does not eliminate every race condition: results can still depend on timing. section 2.1.2 section 2.5
def coroutine():
  yield "foo"
  yield "bar"
  return "end"
coro = coroutine()
while True:
  try:
    print(next(coro)) # "foo" then "bar"
  except StopIteration as e:
    print(e.value) # "end"
    break

The Python coroutine yields two values, retaining its place between calls to next(). On completion, StopIteration carries its return value. This shows how execution can pause and resume without creating another thread. section 2.2

How The Coder Cafe covers it

How deep it goes
Works through the concurrency–parallelism distinction and coroutine resumption, then summarizes shared-resource and timing hazards without detailing their implementations.
Examples
Uses a coffee shop’s waiters and queues alongside a Python yield-and-next coroutine listing.
What it assumes
Defines concurrency against parallelism before introducing coordination primitives, without specifying prerequisite knowledge.
What you can do after
Evaluate/understand when independent task structure helps, whether parallel execution is necessary, and where coordination or timing can cause failures.
How it connects
Connects parallelism and asynchronous I/O to coroutines, mutexes, semaphores, data races, and race conditions.

Also explained in

Compare how 31 books explain concurrency →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Coder Cafe. The section links point to the parts of the book it draws on.