Runs the ``callback`` at the time ``deadline`` from the I/O loop. Returns an opaque handle that may be passed to `remove_timeout` to cancel. ``deadline`` may be a number denoting a time (on the same scale as `IOLoop.time`, normally `time.time`), or a `dateti
(
self,
deadline: Union[float, datetime.timedelta],
callback: Callable,
*args: Any,
**kwargs: Any,
)
| 561 | return time.time() |
| 562 | |
| 563 | def add_timeout( |
| 564 | self, |
| 565 | deadline: Union[float, datetime.timedelta], |
| 566 | callback: Callable, |
| 567 | *args: Any, |
| 568 | **kwargs: Any, |
| 569 | ) -> object: |
| 570 | """Runs the ``callback`` at the time ``deadline`` from the I/O loop. |
| 571 | |
| 572 | Returns an opaque handle that may be passed to |
| 573 | `remove_timeout` to cancel. |
| 574 | |
| 575 | ``deadline`` may be a number denoting a time (on the same |
| 576 | scale as `IOLoop.time`, normally `time.time`), or a |
| 577 | `datetime.timedelta` object for a deadline relative to the |
| 578 | current time. Since Tornado 4.0, `call_later` is a more |
| 579 | convenient alternative for the relative case since it does not |
| 580 | require a timedelta object. |
| 581 | |
| 582 | Note that it is not safe to call `add_timeout` from other threads. |
| 583 | Instead, you must use `add_callback` to transfer control to the |
| 584 | `IOLoop`'s thread, and then call `add_timeout` from there. |
| 585 | |
| 586 | Subclasses of IOLoop must implement either `add_timeout` or |
| 587 | `call_at`; the default implementations of each will call |
| 588 | the other. `call_at` is usually easier to implement, but |
| 589 | subclasses that wish to maintain compatibility with Tornado |
| 590 | versions prior to 4.0 must use `add_timeout` instead. |
| 591 | |
| 592 | .. versionchanged:: 4.0 |
| 593 | Now passes through ``*args`` and ``**kwargs`` to the callback. |
| 594 | """ |
| 595 | if isinstance(deadline, numbers.Real): |
| 596 | return self.call_at(deadline, callback, *args, **kwargs) |
| 597 | elif isinstance(deadline, datetime.timedelta): |
| 598 | return self.call_at( |
| 599 | self.time() + deadline.total_seconds(), callback, *args, **kwargs |
| 600 | ) |
| 601 | else: |
| 602 | raise TypeError("Unsupported deadline %r" % deadline) |
| 603 | |
| 604 | def call_later( |
| 605 | self, delay: float, callback: Callable, *args: Any, **kwargs: Any |