MCPcopy Index your code
hub / github.com/python/cpython / ensure_future

Function ensure_future

Lib/asyncio/tasks.py:707–736  ·  view source on GitHub ↗

Wrap a coroutine or an awaitable in a future. If the argument is a Future, it is returned directly.

(coro_or_future, *, loop=None)

Source from the content-addressed store, hash-verified

705
706
707def 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
739class _GatheringFuture(futures.Future):

Callers 5

wait_forFunction · 0.85
__init__Method · 0.85
gatherFunction · 0.85
shieldFunction · 0.85
callbackFunction · 0.85

Calls 5

_wrap_awaitableFunction · 0.85
_get_loopMethod · 0.80
get_event_loopMethod · 0.45
create_taskMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…