mirror of
https://github.com/python/cpython.git
synced 2025-10-07 07:31:46 +00:00
bpo-39529: Deprecate creating new event loop in asyncio.get_event_loop() (GH-23554)
asyncio.get_event_loop() emits now a deprecation warning when it creates a new event loop. In future releases it will became an alias of asyncio.get_running_loop().
This commit is contained in:
parent
face87c94e
commit
172c0f2752
16 changed files with 570 additions and 185 deletions
|
@ -549,7 +549,7 @@ def as_completed(fs, *, timeout=None):
|
|||
from .queues import Queue # Import here to avoid circular import problem.
|
||||
done = Queue()
|
||||
|
||||
loop = events.get_event_loop()
|
||||
loop = events._get_event_loop()
|
||||
todo = {ensure_future(f, loop=loop) for f in set(fs)}
|
||||
timeout_handle = None
|
||||
|
||||
|
@ -616,23 +616,26 @@ def ensure_future(coro_or_future, *, loop=None):
|
|||
|
||||
If the argument is a Future, it is returned directly.
|
||||
"""
|
||||
if coroutines.iscoroutine(coro_or_future):
|
||||
if loop is None:
|
||||
loop = events.get_event_loop()
|
||||
task = loop.create_task(coro_or_future)
|
||||
if task._source_traceback:
|
||||
del task._source_traceback[-1]
|
||||
return task
|
||||
elif futures.isfuture(coro_or_future):
|
||||
return _ensure_future(coro_or_future, loop=loop)
|
||||
|
||||
|
||||
def _ensure_future(coro_or_future, *, loop=None):
|
||||
if futures.isfuture(coro_or_future):
|
||||
if loop is not None and loop is not futures._get_loop(coro_or_future):
|
||||
raise ValueError('The future belongs to a different loop than '
|
||||
'the one specified as the loop argument')
|
||||
'the one specified as the loop argument')
|
||||
return coro_or_future
|
||||
elif inspect.isawaitable(coro_or_future):
|
||||
return ensure_future(_wrap_awaitable(coro_or_future), loop=loop)
|
||||
else:
|
||||
raise TypeError('An asyncio.Future, a coroutine or an awaitable is '
|
||||
'required')
|
||||
|
||||
if not coroutines.iscoroutine(coro_or_future):
|
||||
if inspect.isawaitable(coro_or_future):
|
||||
coro_or_future = _wrap_awaitable(coro_or_future)
|
||||
else:
|
||||
raise TypeError('An asyncio.Future, a coroutine or an awaitable '
|
||||
'is required')
|
||||
|
||||
if loop is None:
|
||||
loop = events._get_event_loop(stacklevel=4)
|
||||
return loop.create_task(coro_or_future)
|
||||
|
||||
|
||||
@types.coroutine
|
||||
|
@ -655,7 +658,8 @@ class _GatheringFuture(futures.Future):
|
|||
cancelled.
|
||||
"""
|
||||
|
||||
def __init__(self, children, *, loop=None):
|
||||
def __init__(self, children, *, loop):
|
||||
assert loop is not None
|
||||
super().__init__(loop=loop)
|
||||
self._children = children
|
||||
self._cancel_requested = False
|
||||
|
@ -706,7 +710,7 @@ def gather(*coros_or_futures, return_exceptions=False):
|
|||
gather won't cancel any other awaitables.
|
||||
"""
|
||||
if not coros_or_futures:
|
||||
loop = events.get_event_loop()
|
||||
loop = events._get_event_loop()
|
||||
outer = loop.create_future()
|
||||
outer.set_result([])
|
||||
return outer
|
||||
|
@ -773,7 +777,7 @@ def gather(*coros_or_futures, return_exceptions=False):
|
|||
loop = None
|
||||
for arg in coros_or_futures:
|
||||
if arg not in arg_to_fut:
|
||||
fut = ensure_future(arg, loop=loop)
|
||||
fut = _ensure_future(arg, loop=loop)
|
||||
if loop is None:
|
||||
loop = futures._get_loop(fut)
|
||||
if fut is not arg:
|
||||
|
@ -823,7 +827,7 @@ def shield(arg):
|
|||
except CancelledError:
|
||||
res = None
|
||||
"""
|
||||
inner = ensure_future(arg)
|
||||
inner = _ensure_future(arg)
|
||||
if inner.done():
|
||||
# Shortcut.
|
||||
return inner
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue