Coroutine that completes after a given time (in seconds).
(delay, result=None)
| 685 | |
| 686 | |
| 687 | async def sleep(delay, result=None): |
| 688 | """Coroutine that completes after a given time (in seconds).""" |
| 689 | if delay <= 0: |
| 690 | await __sleep0() |
| 691 | return result |
| 692 | |
| 693 | if math.isnan(delay): |
| 694 | raise ValueError("Invalid delay: NaN (not a number)") |
| 695 | |
| 696 | loop = events.get_running_loop() |
| 697 | future = loop.create_future() |
| 698 | h = loop.call_later(delay, |
| 699 | futures._set_result_unless_cancelled, |
| 700 | future, result) |
| 701 | try: |
| 702 | return await future |
| 703 | finally: |
| 704 | h.cancel() |
| 705 | |
| 706 | |
| 707 | def ensure_future(coro_or_future, *, loop=None): |