asyncio: async() function is deprecated in favour of ensure_future().

This commit is contained in:
Yury Selivanov 2015-05-11 14:48:38 -04:00
parent 740169cd24
commit 59eb9a4da5
7 changed files with 57 additions and 34 deletions

View file

@ -3,7 +3,7 @@
__all__ = ['Task',
'FIRST_COMPLETED', 'FIRST_EXCEPTION', 'ALL_COMPLETED',
'wait', 'wait_for', 'as_completed', 'sleep', 'async',
'gather', 'shield',
'gather', 'shield', 'ensure_future',
]
import concurrent.futures
@ -12,6 +12,7 @@ import inspect
import linecache
import sys
import traceback
import warnings
import weakref
from . import coroutines
@ -327,7 +328,7 @@ def wait(fs, *, loop=None, timeout=None, return_when=ALL_COMPLETED):
if loop is None:
loop = events.get_event_loop()
fs = {async(f, loop=loop) for f in set(fs)}
fs = {ensure_future(f, loop=loop) for f in set(fs)}
return (yield from _wait(fs, timeout, return_when, loop))
@ -361,7 +362,7 @@ def wait_for(fut, timeout, *, loop=None):
timeout_handle = loop.call_later(timeout, _release_waiter, waiter)
cb = functools.partial(_release_waiter, waiter)
fut = async(fut, loop=loop)
fut = ensure_future(fut, loop=loop)
fut.add_done_callback(cb)
try:
@ -449,7 +450,7 @@ def as_completed(fs, *, loop=None, timeout=None):
if isinstance(fs, futures.Future) or coroutines.iscoroutine(fs):
raise TypeError("expect a list of futures, not %s" % type(fs).__name__)
loop = loop if loop is not None else events.get_event_loop()
todo = {async(f, loop=loop) for f in set(fs)}
todo = {ensure_future(f, loop=loop) for f in set(fs)}
from .queues import Queue # Import here to avoid circular import problem.
done = Queue(loop=loop)
timeout_handle = None
@ -499,6 +500,20 @@ def sleep(delay, result=None, *, loop=None):
def async(coro_or_future, *, loop=None):
"""Wrap a coroutine in a future.
If the argument is a Future, it is returned directly.
This function is deprecated in 3.5. Use asyncio.ensure_future() instead.
"""
warnings.warn("asyncio.async() function is deprecated, use ensure_future()",
DeprecationWarning)
return ensure_future(coro_or_future, loop=loop)
def ensure_future(coro_or_future, *, loop=None):
"""Wrap a coroutine in a future.
If the argument is a Future, it is returned directly.
"""
if isinstance(coro_or_future, futures.Future):
@ -564,7 +579,7 @@ def gather(*coros_or_futures, loop=None, return_exceptions=False):
arg_to_fut = {}
for arg in set(coros_or_futures):
if not isinstance(arg, futures.Future):
fut = async(arg, loop=loop)
fut = ensure_future(arg, loop=loop)
if loop is None:
loop = fut._loop
# The caller cannot control this future, the "destroy pending task"
@ -640,7 +655,7 @@ def shield(arg, *, loop=None):
except CancelledError:
res = None
"""
inner = async(arg, loop=loop)
inner = ensure_future(arg, loop=loop)
if inner.done():
# Shortcut.
return inner