15
Lesson 42 of 44

Concurrency

Learn to do several things at once, or to make it look that way. In this lesson you will tell concurrency from parallelism, learn about the GIL, use threads with threading and locks, the concurrent.futures module, queue queues and asyncio coroutines, and see how to avoid race conditions and deadlocks. The browser corrector cannot create real threads, so the exercises simulate schedulers, queues, semaphores, pools and event loops in a deterministic way.

  • 5 theory articles
  • 10 practice exercises
Start lesson

Fundamentals concepts 5

Concurrency, parallelism and the GIL

A sequential program does one thing after another. But many programs spend most of their time waiting : for a server, a disk, a…

Theory
Read more

Threads with threading

A thread is a flow of execution inside a process. All the threads of a process share memory (variables, lists…). The threading…

Theory
Read more

concurrent.futures and queues

Creating and waiting for threads by hand is tedious and error-prone. The concurrent.futures module offers a higher-level…

Theory
Read more

asyncio: coroutines and the event loop

asyncio offers concurrency in a single thread . Instead of the operating system switching threads whenever it wants, you decide…

Theory
Read more

Deadlocks and good practices

Locks solve race conditions, but they introduce another danger: the deadlock .

Theory
Read more

Practice exercises 10

Round-robin tasks

Create a Python program that shares time between several tasks in turns ( round-robin ), as a task scheduler does. Each task is a…

Exercise Easy
Solve it

Amdahl's law

Adding processors does not speed up a program indefinitely: the part that cannot be parallelized limits the improvement. Amdahl's…

Exercise Easy
Solve it

Simulated race condition

A race condition happens when several threads read and modify the same data and the result depends on the order in which they…

Exercise Medium
Solve it

Producer and consumer

The producer-consumer pattern connects two parts through a queue with limited capacity. Use queue.Queue(maxsize=...) from the…

Exercise Medium
Solve it

Semaphore with a waiting queue

A semaphore limits how many threads can use a resource at the same time: it has a number of permits . If none are left, the…

Exercise Medium
Solve it

Splitting work into chunks

To take advantage of several processors you have to divide the work into independent chunks, process them separately and combine…

Exercise Medium
Solve it

Worker pool

A worker pool (like ThreadPoolExecutor ) shares a list of jobs among a fixed number of workers: each job is taken by the first…

Exercise Hard
Solve it

Event loop with a virtual clock

This is how asyncio.sleep works inside: an event loop keeps a schedule of sleeping tasks and always wakes up the one that is due…

Exercise Hard
Solve it

Deadlock risk

A deadlock happens when two threads wait forever: each one holds a lock the other one needs. It can be detected in advance : if…

Exercise Hard
Solve it

Tasks with dependencies

With enough workers, independent tasks run at the same time, but a task cannot start until the ones it depends on have finished…

Exercise Hard
Solve it