11 Synchronization

This chapter covers

  • Single-threaded concurrency issues
  • Using locks to protect critical sections
  • Using semaphores to limit concurrency
  • Using events to notify tasks
  • Using conditions to notify tasks and acquire a resource

When we write applications using multiple threads and multiple processes, we need to worry about race conditions when using non-atomic operations. Something as simple as incrementing an integer concurrently can cause subtle, hard-to-reproduce bugs. When we are using asyncio, however, we’re always using a single thread (unless we’re interoperating with multithreading and multiprocessing), so doesn’t that mean we don’t need to worry about race conditions? It turns out it is not quite so simple.

While certain ...

Get Python Concurrency with asyncio now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.