Wrap a coroutine or an awaitable in a future. If the argument is a Future, it is returned directly.
(coro_or_future, *, loop=None)
| 705 | |
| 706 | |
| 707 | def ensure_future(coro_or_future, *, loop=None): |
| 708 | """Wrap a coroutine or an awaitable in a future. |
| 709 | |
| 710 | If the argument is a Future, it is returned directly. |
| 711 | """ |
| 712 | if futures.isfuture(coro_or_future): |
| 713 | if loop is not None and loop is not futures._get_loop(coro_or_future): |
| 714 | raise ValueError('The future belongs to a different loop than ' |
| 715 | 'the one specified as the loop argument') |
| 716 | return coro_or_future |
| 717 | should_close = True |
| 718 | if not coroutines.iscoroutine(coro_or_future): |
| 719 | if inspect.isawaitable(coro_or_future): |
| 720 | async def _wrap_awaitable(awaitable): |
| 721 | return await awaitable |
| 722 | |
| 723 | coro_or_future = _wrap_awaitable(coro_or_future) |
| 724 | should_close = False |
| 725 | else: |
| 726 | raise TypeError('An asyncio.Future, a coroutine or an awaitable ' |
| 727 | 'is required') |
| 728 | |
| 729 | if loop is None: |
| 730 | loop = events.get_event_loop() |
| 731 | try: |
| 732 | return loop.create_task(coro_or_future) |
| 733 | except RuntimeError: |
| 734 | if should_close: |
| 735 | coro_or_future.close() |
| 736 | raise |
| 737 | |
| 738 | |
| 739 | class _GatheringFuture(futures.Future): |
no test coverage detected
searching dependent graphs…