Primitive lock objects. A primitive lock is a synchronization primitive that is not owned by a particular task when locked. A primitive lock is in one of two states, 'locked' or 'unlocked'. It is created in the unlocked state. It has two basic methods, acquire() and release()
| 41 | |
| 42 | |
| 43 | class Lock(_ContextManagerMixin, _LoopBoundMixin): |
| 44 | """Primitive lock objects. |
| 45 | |
| 46 | A primitive lock is a synchronization primitive that is not owned |
| 47 | by a particular task when locked. A primitive lock is in one |
| 48 | of two states, 'locked' or 'unlocked'. |
| 49 | |
| 50 | It is created in the unlocked state. It has two basic methods, |
| 51 | acquire() and release(). When the state is unlocked, acquire() |
| 52 | changes the state to locked and returns immediately. When the |
| 53 | state is locked, acquire() blocks until a call to release() in |
| 54 | another task changes it to unlocked, then the acquire() call |
| 55 | resets it to locked and returns. The release() method should only |
| 56 | be called in the locked state; it changes the state to unlocked |
| 57 | and returns immediately. If an attempt is made to release an |
| 58 | unlocked lock, a RuntimeError will be raised. |
| 59 | |
| 60 | When more than one task is blocked in acquire() waiting for |
| 61 | the state to turn to unlocked, only one task proceeds when a |
| 62 | release() call resets the state to unlocked; successive release() |
| 63 | calls will unblock tasks in FIFO order. |
| 64 | |
| 65 | Locks also support the asynchronous context management protocol. |
| 66 | 'async with lock' statement should be used. |
| 67 | |
| 68 | Usage: |
| 69 | |
| 70 | lock = Lock() |
| 71 | ... |
| 72 | await lock.acquire() |
| 73 | try: |
| 74 | ... |
| 75 | finally: |
| 76 | lock.release() |
| 77 | |
| 78 | Context manager usage: |
| 79 | |
| 80 | lock = Lock() |
| 81 | ... |
| 82 | async with lock: |
| 83 | ... |
| 84 | |
| 85 | Lock objects can be tested for locking state: |
| 86 | |
| 87 | if not lock.locked(): |
| 88 | await lock.acquire() |
| 89 | else: |
| 90 | # lock is acquired |
| 91 | ... |
| 92 | |
| 93 | """ |
| 94 | |
| 95 | def __init__(self) -> None: |
| 96 | self._waiters: Optional[collections.deque[Any]] = None |
| 97 | self._locked = False |
| 98 | |
| 99 | def __repr__(self) -> str: |
| 100 | res = super().__repr__() |
no outgoing calls
no test coverage detected