Yield `Notify` objects as soon as they are received from the database. :param timeout: maximum amount of time to wait for notifications. `!None` means no timeout. :param stop_after: stop after receiving this number of notifications. You might actuall
(
self, *, timeout: float | None = None, stop_after: int | None = None
)
| 393 | yield tx |
| 394 | |
| 395 | async def notifies( |
| 396 | self, *, timeout: float | None = None, stop_after: int | None = None |
| 397 | ) -> AsyncGenerator[Notify]: |
| 398 | """ |
| 399 | Yield `Notify` objects as soon as they are received from the database. |
| 400 | |
| 401 | :param timeout: maximum amount of time to wait for notifications. |
| 402 | `!None` means no timeout. |
| 403 | :param stop_after: stop after receiving this number of notifications. |
| 404 | You might actually receive more than this number if more than one |
| 405 | notifications arrives in the same packet. |
| 406 | """ |
| 407 | # Allow interrupting the wait with a signal by reducing a long timeout |
| 408 | # into shorter intervals. |
| 409 | if timeout is not None: |
| 410 | deadline = monotonic() + timeout |
| 411 | interval = min(timeout, _WAIT_INTERVAL) |
| 412 | else: |
| 413 | deadline = None |
| 414 | interval = _WAIT_INTERVAL |
| 415 | |
| 416 | nreceived = 0 |
| 417 | |
| 418 | if self._notify_handlers: |
| 419 | warnings.warn( |
| 420 | "using 'notifies()' together with notifies handlers on the" |
| 421 | " same connection is not reliable." |
| 422 | " Please use only one of these methods", |
| 423 | RuntimeWarning, |
| 424 | ) |
| 425 | |
| 426 | async with self.lock: |
| 427 | enc = self.pgconn._encoding |
| 428 | |
| 429 | # Remove the backlog deque for the duration of this critical |
| 430 | # section to avoid reporting notifies twice. |
| 431 | self._notifies_backlog, d = None, self._notifies_backlog |
| 432 | |
| 433 | try: |
| 434 | while True: |
| 435 | # if notifies were received when the generator was off, |
| 436 | # return them in a first batch. |
| 437 | if d: |
| 438 | while d: |
| 439 | yield d.popleft() |
| 440 | nreceived += 1 |
| 441 | else: |
| 442 | try: |
| 443 | pgns = await self.wait( |
| 444 | notifies(self.pgconn), interval=interval |
| 445 | ) |
| 446 | except e._NO_TRACEBACK as ex: |
| 447 | raise ex.with_traceback(None) |
| 448 | |
| 449 | # Emit the notifications received. |
| 450 | for pgn in pgns: |
| 451 | yield Notify( |
| 452 | pgn.relname.decode(enc), |