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()
| 21 | |
| 22 | |
| 23 | class Lock(_ContextManagerMixin, mixins._LoopBoundMixin): |
| 24 | """Primitive lock objects. |
| 25 | |
| 26 | A primitive lock is a synchronization primitive that is not owned |
| 27 | by a particular task when locked. A primitive lock is in one |
| 28 | of two states, 'locked' or 'unlocked'. |
| 29 | |
| 30 | It is created in the unlocked state. It has two basic methods, |
| 31 | acquire() and release(). When the state is unlocked, acquire() |
| 32 | changes the state to locked and returns immediately. When the |
| 33 | state is locked, acquire() blocks until a call to release() in |
| 34 | another task changes it to unlocked, then the acquire() call |
| 35 | resets it to locked and returns. The release() method should only |
| 36 | be called in the locked state; it changes the state to unlocked |
| 37 | and returns immediately. If an attempt is made to release an |
| 38 | unlocked lock, a RuntimeError will be raised. |
| 39 | |
| 40 | When more than one task is blocked in acquire() waiting for |
| 41 | the state to turn to unlocked, only one task proceeds when a |
| 42 | release() call resets the state to unlocked; successive release() |
| 43 | calls will unblock tasks in FIFO order. |
| 44 | |
| 45 | Locks also support the asynchronous context management protocol. |
| 46 | 'async with lock' statement should be used. |
| 47 | |
| 48 | Usage: |
| 49 | |
| 50 | lock = Lock() |
| 51 | ... |
| 52 | await lock.acquire() |
| 53 | try: |
| 54 | ... |
| 55 | finally: |
| 56 | lock.release() |
| 57 | |
| 58 | Context manager usage: |
| 59 | |
| 60 | lock = Lock() |
| 61 | ... |
| 62 | async with lock: |
| 63 | ... |
| 64 | |
| 65 | Lock objects can be tested for locking state: |
| 66 | |
| 67 | if not lock.locked(): |
| 68 | await lock.acquire() |
| 69 | else: |
| 70 | # lock is acquired |
| 71 | ... |
| 72 | |
| 73 | """ |
| 74 | |
| 75 | def __init__(self): |
| 76 | self._waiters = None |
| 77 | self._locked = False |
| 78 | |
| 79 | def __repr__(self): |
| 80 | res = super().__repr__() |
no outgoing calls
no test coverage detected
searching dependent graphs…