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)
| 246 | return f'<{res[1:-1]} [{extra}]>' |
| 247 | |
| 248 | async def wait(self): |
| 249 | """Wait until notified. |
| 250 | |
| 251 | If the calling task has not acquired the lock when this |
| 252 | method is called, a RuntimeError is raised. |
| 253 | |
| 254 | This method releases the underlying lock, and then blocks |
| 255 | until it is awakened by a notify() or notify_all() call for |
| 256 | the same condition variable in another task. Once |
| 257 | awakened, it re-acquires the lock and returns True. |
| 258 | |
| 259 | This method may return spuriously, |
| 260 | which is why the caller should always |
| 261 | re-check the state and be prepared to wait() again. |
| 262 | """ |
| 263 | if not self.locked(): |
| 264 | raise RuntimeError('cannot wait on un-acquired lock') |
| 265 | |
| 266 | fut = self._get_loop().create_future() |
| 267 | self.release() |
| 268 | try: |
| 269 | try: |
| 270 | self._waiters.append(fut) |
| 271 | try: |
| 272 | await fut |
| 273 | return True |
| 274 | finally: |
| 275 | self._waiters.remove(fut) |
| 276 | |
| 277 | finally: |
| 278 | # Must re-acquire lock even if wait is cancelled. |
| 279 | # We only catch CancelledError here, since we don't want any |
| 280 | # other (fatal) errors with the future to cause us to spin. |
| 281 | err = None |
| 282 | while True: |
| 283 | try: |
| 284 | await self.acquire() |
| 285 | break |
| 286 | except exceptions.CancelledError as e: |
| 287 | err = e |
| 288 | |
| 289 | if err is not None: |
| 290 | try: |
| 291 | raise err # Re-raise most recent exception instance. |
| 292 | finally: |
| 293 | err = None # Break reference cycles. |
| 294 | except BaseException: |
| 295 | # Any error raised out of here _may_ have occurred after this Task |
| 296 | # believed to have been successfully notified. |
| 297 | # Make sure to notify another Task instead. This may result |
| 298 | # in a "spurious wakeup", which is allowed as part of the |
| 299 | # Condition Variable protocol. |
| 300 | self._notify(1) |
| 301 | raise |
| 302 | |
| 303 | async def wait_for(self, predicate): |
| 304 | """Wait until a predicate becomes true. |