8

According to asyncio synchronization primitives, there are synchronization methods.

  • I am getting confused about why we need synchronization in asyncio?
  • I mean, asyncio is asynchronous.
  • Is it meaningful to add something synchronous in asynchronization?
Community
  • 1
  • 1
HIPPO LD
  • 477
  • 5
  • 20

1 Answers1

11

Synchronization primitives don't make your code synchronous, they make coroutines in your code synchronized.

Few examples:

  • You may want to start/continue some coroutine only when another coroutine allows it (asyncio.Event)
  • You may want some part of your code to be executed only by single coroutine at the same time and other to wait for their turn (asyncio.Lock)
  • You may want some part of your code to be executed only by limited number on coroutines at the same time (asyncio.Semaphore)

Take a look at a practical example of using asyncio.Semaphore.

Mikhail Gerasimov
  • 36,989
  • 16
  • 116
  • 159