Does the lock in asyncio.Condition have other purpose besides compatibility with threading.Condition?

Clash Royale CLAN TAG#URR8PPPDoes the lock in asyncio.Condition have other purpose besides compatibility with threading.Condition?
I'd like to ask about asyncio.Condition. I'm not familiar with the concept, but I know and understand locks, semaphores, and queues since my student years.
I could not find a good explanation or typical use cases, just this example. I looked at the source. The core fnctionality is achieved with a FIFO of futures. Each waiting coroutine adds a new future and awaits it. Another coroutine may call notify() which sets the result of one or optionally more futures from the FIFO and that wakes up the same number of waiting coroutines. Really simple up to this point.
notify()
However, the implementation and the usage is more complicated than this. A waiting coroutine must first acquire a lock associated with the condition in order to be able to wait (and the wait() releases it while waiting). Also the notifier must acquire a lock to be able to notify(). This leads to with statement before each operation:
wait()
with
async with condition:
# condition operation (wait or notify)
or else a RuntimeError occurrs.
RuntimeError
I do not understand the point of having this lock. What resource do we need to protect with the lock? In asyncio there could be always only one coroutine executing in the event loop, there are no "critical sections" as known from threading.
Is this lock really needed (why?) or is it for compatibility with threading code only?
My first idea was it is for the compatibility, but in such case why didn't they remove the lock while preserving the usage? i.e. making
async with condition:
basically an optional no-op.
This question has not received enough attention.
1 Answer
1
This mechanism is very similar to Java. java.lang.Object has the methods wait and notify, and in order to call them, you must synchronize on the object (i.e. hold the lock, or monitor in Java terms).
java.lang.Object
wait
notify
The same question has been asked about why it is necessary to hold the locks in Java, please find your answer here: Why do we need to synchronize on the same object for notify() to work
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.