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






























