Wait until notified. If the calling task has not acquired the lock when this method is called, a RuntimeError is raised. This method releases the underlying lock, and then blocks until it is awakened by a notify() or notify_all() call for the same condition
(self)
| 206 | return f"<{res[1:-1]} [{extra}]>" |
| 207 | |
| 208 | async def wait(self) -> bool: |
| 209 | """Wait until notified. |
| 210 | |
| 211 | If the calling task has not acquired the lock when this |
| 212 | method is called, a RuntimeError is raised. |
| 213 | |
| 214 | This method releases the underlying lock, and then blocks |
| 215 | until it is awakened by a notify() or notify_all() call for |
| 216 | the same condition variable in another task. Once |
| 217 | awakened, it re-acquires the lock and returns True. |
| 218 | |
| 219 | This method may return spuriously, |
| 220 | which is why the caller should always |
| 221 | re-check the state and be prepared to wait() again. |
| 222 | """ |
| 223 | if not self.locked(): |
| 224 | raise RuntimeError("cannot wait on un-acquired lock") |
| 225 | |
| 226 | fut = self._get_loop().create_future() |
| 227 | self.release() |
| 228 | try: |
| 229 | try: |
| 230 | self._waiters.append(fut) |
| 231 | try: |
| 232 | await fut |
| 233 | return True |
| 234 | finally: |
| 235 | self._waiters.remove(fut) |
| 236 | |
| 237 | finally: |
| 238 | # Must re-acquire lock even if wait is cancelled. |
| 239 | # We only catch CancelledError here, since we don't want any |
| 240 | # other (fatal) errors with the future to cause us to spin. |
| 241 | err = None |
| 242 | while True: |
| 243 | try: |
| 244 | await self.acquire() |
| 245 | break |
| 246 | except exceptions.CancelledError as e: |
| 247 | err = e |
| 248 | |
| 249 | if err is not None: |
| 250 | try: |
| 251 | raise err # Re-raise most recent exception instance. |
| 252 | finally: |
| 253 | err = None # Break reference cycles. |
| 254 | except BaseException: |
| 255 | # Any error raised out of here _may_ have occurred after this Task |
| 256 | # believed to have been successfully notified. |
| 257 | # Make sure to notify another Task instead. This may result |
| 258 | # in a "spurious wakeup", which is allowed as part of the |
| 259 | # Condition Variable protocol. |
| 260 | self._notify(1) |
| 261 | raise |
| 262 | |
| 263 | async def wait_for(self, predicate: Any) -> Coroutine[Any, Any, Any]: |
| 264 | """Wait until a predicate becomes true. |
no test coverage detected