diff options
author | Yury Selivanov <yselivanov@sprymix.com> | 2015-05-11 14:48:38 -0400 |
---|---|---|
committer | Yury Selivanov <yselivanov@sprymix.com> | 2015-05-11 14:48:38 -0400 |
commit | 59eb9a4da504315345b5b0d8fb8eebddc22ccb92 (patch) | |
tree | d86de5b0adb841e211f37adbe00ea8f9b25b343f /Lib/asyncio/tasks.py | |
parent | 740169cd24fc108913e4480e98e608f0517a7b8a (diff) | |
download | cpython-git-59eb9a4da504315345b5b0d8fb8eebddc22ccb92.tar.gz |
asyncio: async() function is deprecated in favour of ensure_future().
Diffstat (limited to 'Lib/asyncio/tasks.py')
-rw-r--r-- | Lib/asyncio/tasks.py | 27 |
1 files changed, 21 insertions, 6 deletions
diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index 4f19a252ff..f617b62be2 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -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 @@ -500,6 +501,20 @@ 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): if loop is not None and loop is not coro_or_future._loop: @@ -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 |