Acquire a lock. This method blocks until the lock is unlocked, then sets it to locked and returns True.
(self)
| 91 | return self._locked |
| 92 | |
| 93 | async def acquire(self): |
| 94 | """Acquire a lock. |
| 95 | |
| 96 | This method blocks until the lock is unlocked, then sets it to |
| 97 | locked and returns True. |
| 98 | """ |
| 99 | if (not self._locked and (self._waiters is None or |
| 100 | all(w.cancelled() for w in self._waiters))): |
| 101 | self._locked = True |
| 102 | return True |
| 103 | |
| 104 | if self._waiters is None: |
| 105 | self._waiters = collections.deque() |
| 106 | fut = self._get_loop().create_future() |
| 107 | self._waiters.append(fut) |
| 108 | |
| 109 | # Finally block should be called before the CancelledError |
| 110 | # handling as we don't want CancelledError to call |
| 111 | # _wake_up_first() and attempt to wake up itself. |
| 112 | try: |
| 113 | try: |
| 114 | await fut |
| 115 | finally: |
| 116 | self._waiters.remove(fut) |
| 117 | except exceptions.CancelledError: |
| 118 | if not self._locked: |
| 119 | self._wake_up_first() |
| 120 | raise |
| 121 | |
| 122 | self._locked = True |
| 123 | return True |
| 124 | |
| 125 | def release(self): |
| 126 | """Release a lock. |
no test coverage detected