Coroutine that completes after a given time (in seconds).
(delay, result=None)
| 635 | |
| 636 | |
| 637 | async def sleep(delay, result=None): |
| 638 | """Coroutine that completes after a given time (in seconds).""" |
| 639 | if delay <= 0: |
| 640 | await __sleep0() |
| 641 | return result |
| 642 | |
| 643 | loop = events.get_running_loop() |
| 644 | future = loop.create_future() |
| 645 | h = loop.call_later(delay, |
| 646 | futures._set_result_unless_cancelled, |
| 647 | future, result) |
| 648 | try: |
| 649 | return await future |
| 650 | finally: |
| 651 | h.cancel() |
| 652 | |
| 653 | |
| 654 | def ensure_future(coro_or_future, *, loop=None): |