Deprecate async() function in favour of ensure_future()
This commit is contained in:
parent
1181bc193a
commit
53edb85e6e
|
@ -315,7 +315,7 @@ class BaseEventLoop(events.AbstractEventLoop):
|
||||||
self._check_closed()
|
self._check_closed()
|
||||||
|
|
||||||
new_task = not isinstance(future, futures.Future)
|
new_task = not isinstance(future, futures.Future)
|
||||||
future = tasks.async(future, loop=self)
|
future = tasks.ensure_future(future, loop=self)
|
||||||
if new_task:
|
if new_task:
|
||||||
# An exception is raised if the future didn't complete, so there
|
# An exception is raised if the future didn't complete, so there
|
||||||
# is no need to log the "destroy pending task" message
|
# is no need to log the "destroy pending task" message
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
__all__ = ['Task',
|
__all__ = ['Task',
|
||||||
'FIRST_COMPLETED', 'FIRST_EXCEPTION', 'ALL_COMPLETED',
|
'FIRST_COMPLETED', 'FIRST_EXCEPTION', 'ALL_COMPLETED',
|
||||||
'wait', 'wait_for', 'as_completed', 'sleep', 'async',
|
'wait', 'wait_for', 'as_completed', 'sleep', 'async',
|
||||||
'gather', 'shield',
|
'gather', 'shield', 'ensure_future',
|
||||||
]
|
]
|
||||||
|
|
||||||
import concurrent.futures
|
import concurrent.futures
|
||||||
|
@ -12,6 +12,7 @@ import inspect
|
||||||
import linecache
|
import linecache
|
||||||
import sys
|
import sys
|
||||||
import traceback
|
import traceback
|
||||||
|
import warnings
|
||||||
import weakref
|
import weakref
|
||||||
|
|
||||||
from . import coroutines
|
from . import coroutines
|
||||||
|
@ -327,7 +328,7 @@ def wait(fs, *, loop=None, timeout=None, return_when=ALL_COMPLETED):
|
||||||
if loop is None:
|
if loop is None:
|
||||||
loop = events.get_event_loop()
|
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))
|
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)
|
timeout_handle = loop.call_later(timeout, _release_waiter, waiter)
|
||||||
cb = functools.partial(_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)
|
fut.add_done_callback(cb)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -449,7 +450,7 @@ def as_completed(fs, *, loop=None, timeout=None):
|
||||||
if isinstance(fs, futures.Future) or coroutines.iscoroutine(fs):
|
if isinstance(fs, futures.Future) or coroutines.iscoroutine(fs):
|
||||||
raise TypeError("expect a list of futures, not %s" % type(fs).__name__)
|
raise TypeError("expect a list of futures, not %s" % type(fs).__name__)
|
||||||
loop = loop if loop is not None else events.get_event_loop()
|
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.
|
from .queues import Queue # Import here to avoid circular import problem.
|
||||||
done = Queue(loop=loop)
|
done = Queue(loop=loop)
|
||||||
timeout_handle = None
|
timeout_handle = None
|
||||||
|
@ -499,6 +500,20 @@ def sleep(delay, result=None, *, loop=None):
|
||||||
def async(coro_or_future, *, loop=None):
|
def async(coro_or_future, *, loop=None):
|
||||||
"""Wrap a coroutine in a future.
|
"""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()",
|
||||||
|
RuntimeWarning)
|
||||||
|
|
||||||
|
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 the argument is a Future, it is returned directly.
|
||||||
"""
|
"""
|
||||||
if isinstance(coro_or_future, futures.Future):
|
if isinstance(coro_or_future, futures.Future):
|
||||||
|
@ -564,7 +579,7 @@ def gather(*coros_or_futures, loop=None, return_exceptions=False):
|
||||||
arg_to_fut = {}
|
arg_to_fut = {}
|
||||||
for arg in set(coros_or_futures):
|
for arg in set(coros_or_futures):
|
||||||
if not isinstance(arg, futures.Future):
|
if not isinstance(arg, futures.Future):
|
||||||
fut = async(arg, loop=loop)
|
fut = ensure_future(arg, loop=loop)
|
||||||
if loop is None:
|
if loop is None:
|
||||||
loop = fut._loop
|
loop = fut._loop
|
||||||
# The caller cannot control this future, the "destroy pending task"
|
# The caller cannot control this future, the "destroy pending task"
|
||||||
|
@ -640,7 +655,7 @@ def shield(arg, *, loop=None):
|
||||||
except CancelledError:
|
except CancelledError:
|
||||||
res = None
|
res = None
|
||||||
"""
|
"""
|
||||||
inner = async(arg, loop=loop)
|
inner = ensure_future(arg, loop=loop)
|
||||||
if inner.done():
|
if inner.done():
|
||||||
# Shortcut.
|
# Shortcut.
|
||||||
return inner
|
return inner
|
||||||
|
|
|
@ -488,7 +488,7 @@ class IocpProactor:
|
||||||
|
|
||||||
future = self._register(ov, listener, finish_accept)
|
future = self._register(ov, listener, finish_accept)
|
||||||
coro = accept_coro(future, conn)
|
coro = accept_coro(future, conn)
|
||||||
tasks.async(coro, loop=self._loop)
|
tasks.ensure_future(coro, loop=self._loop)
|
||||||
return future
|
return future
|
||||||
|
|
||||||
def connect(self, conn, address):
|
def connect(self, conn, address):
|
||||||
|
|
|
@ -504,7 +504,7 @@ class BaseEventLoopTests(test_utils.TestCase):
|
||||||
|
|
||||||
# Test Future.__del__
|
# Test Future.__del__
|
||||||
with mock.patch('asyncio.base_events.logger') as log:
|
with mock.patch('asyncio.base_events.logger') as log:
|
||||||
fut = asyncio.async(zero_error_coro(), loop=self.loop)
|
fut = asyncio.ensure_future(zero_error_coro(), loop=self.loop)
|
||||||
fut.add_done_callback(lambda *args: self.loop.stop())
|
fut.add_done_callback(lambda *args: self.loop.stop())
|
||||||
self.loop.run_forever()
|
self.loop.run_forever()
|
||||||
fut = None # Trigger Future.__del__ or futures._TracebackLogger
|
fut = None # Trigger Future.__del__ or futures._TracebackLogger
|
||||||
|
@ -703,7 +703,7 @@ class BaseEventLoopTests(test_utils.TestCase):
|
||||||
self.set_event_loop(loop)
|
self.set_event_loop(loop)
|
||||||
|
|
||||||
coro = test()
|
coro = test()
|
||||||
task = asyncio.async(coro, loop=loop)
|
task = asyncio.ensure_future(coro, loop=loop)
|
||||||
self.assertIsInstance(task, MyTask)
|
self.assertIsInstance(task, MyTask)
|
||||||
|
|
||||||
# make warnings quiet
|
# make warnings quiet
|
||||||
|
@ -1265,7 +1265,7 @@ class BaseEventLoopWithSelectorTests(test_utils.TestCase):
|
||||||
"took .* seconds$")
|
"took .* seconds$")
|
||||||
|
|
||||||
# slow task
|
# slow task
|
||||||
asyncio.async(stop_loop_coro(self.loop), loop=self.loop)
|
asyncio.ensure_future(stop_loop_coro(self.loop), loop=self.loop)
|
||||||
self.loop.run_forever()
|
self.loop.run_forever()
|
||||||
fmt, *args = m_logger.warning.call_args[0]
|
fmt, *args = m_logger.warning.call_args[0]
|
||||||
self.assertRegex(fmt % tuple(args),
|
self.assertRegex(fmt % tuple(args),
|
||||||
|
|
|
@ -92,11 +92,11 @@ class TaskTests(test_utils.TestCase):
|
||||||
loop.run_until_complete(t)
|
loop.run_until_complete(t)
|
||||||
loop.close()
|
loop.close()
|
||||||
|
|
||||||
def test_async_coroutine(self):
|
def test_ensure_future_coroutine(self):
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def notmuch():
|
def notmuch():
|
||||||
return 'ok'
|
return 'ok'
|
||||||
t = asyncio.async(notmuch(), loop=self.loop)
|
t = asyncio.ensure_future(notmuch(), loop=self.loop)
|
||||||
self.loop.run_until_complete(t)
|
self.loop.run_until_complete(t)
|
||||||
self.assertTrue(t.done())
|
self.assertTrue(t.done())
|
||||||
self.assertEqual(t.result(), 'ok')
|
self.assertEqual(t.result(), 'ok')
|
||||||
|
@ -104,16 +104,16 @@ class TaskTests(test_utils.TestCase):
|
||||||
|
|
||||||
loop = asyncio.new_event_loop()
|
loop = asyncio.new_event_loop()
|
||||||
self.set_event_loop(loop)
|
self.set_event_loop(loop)
|
||||||
t = asyncio.async(notmuch(), loop=loop)
|
t = asyncio.ensure_future(notmuch(), loop=loop)
|
||||||
self.assertIs(t._loop, loop)
|
self.assertIs(t._loop, loop)
|
||||||
loop.run_until_complete(t)
|
loop.run_until_complete(t)
|
||||||
loop.close()
|
loop.close()
|
||||||
|
|
||||||
def test_async_future(self):
|
def test_ensure_future_future(self):
|
||||||
f_orig = asyncio.Future(loop=self.loop)
|
f_orig = asyncio.Future(loop=self.loop)
|
||||||
f_orig.set_result('ko')
|
f_orig.set_result('ko')
|
||||||
|
|
||||||
f = asyncio.async(f_orig)
|
f = asyncio.ensure_future(f_orig)
|
||||||
self.loop.run_until_complete(f)
|
self.loop.run_until_complete(f)
|
||||||
self.assertTrue(f.done())
|
self.assertTrue(f.done())
|
||||||
self.assertEqual(f.result(), 'ko')
|
self.assertEqual(f.result(), 'ko')
|
||||||
|
@ -123,19 +123,19 @@ class TaskTests(test_utils.TestCase):
|
||||||
self.set_event_loop(loop)
|
self.set_event_loop(loop)
|
||||||
|
|
||||||
with self.assertRaises(ValueError):
|
with self.assertRaises(ValueError):
|
||||||
f = asyncio.async(f_orig, loop=loop)
|
f = asyncio.ensure_future(f_orig, loop=loop)
|
||||||
|
|
||||||
loop.close()
|
loop.close()
|
||||||
|
|
||||||
f = asyncio.async(f_orig, loop=self.loop)
|
f = asyncio.ensure_future(f_orig, loop=self.loop)
|
||||||
self.assertIs(f, f_orig)
|
self.assertIs(f, f_orig)
|
||||||
|
|
||||||
def test_async_task(self):
|
def test_ensure_future_task(self):
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def notmuch():
|
def notmuch():
|
||||||
return 'ok'
|
return 'ok'
|
||||||
t_orig = asyncio.Task(notmuch(), loop=self.loop)
|
t_orig = asyncio.Task(notmuch(), loop=self.loop)
|
||||||
t = asyncio.async(t_orig)
|
t = asyncio.ensure_future(t_orig)
|
||||||
self.loop.run_until_complete(t)
|
self.loop.run_until_complete(t)
|
||||||
self.assertTrue(t.done())
|
self.assertTrue(t.done())
|
||||||
self.assertEqual(t.result(), 'ok')
|
self.assertEqual(t.result(), 'ok')
|
||||||
|
@ -145,16 +145,22 @@ class TaskTests(test_utils.TestCase):
|
||||||
self.set_event_loop(loop)
|
self.set_event_loop(loop)
|
||||||
|
|
||||||
with self.assertRaises(ValueError):
|
with self.assertRaises(ValueError):
|
||||||
t = asyncio.async(t_orig, loop=loop)
|
t = asyncio.ensure_future(t_orig, loop=loop)
|
||||||
|
|
||||||
loop.close()
|
loop.close()
|
||||||
|
|
||||||
t = asyncio.async(t_orig, loop=self.loop)
|
t = asyncio.ensure_future(t_orig, loop=self.loop)
|
||||||
self.assertIs(t, t_orig)
|
self.assertIs(t, t_orig)
|
||||||
|
|
||||||
def test_async_neither(self):
|
def test_ensure_future_neither(self):
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(TypeError):
|
||||||
asyncio.async('ok')
|
asyncio.ensure_future('ok')
|
||||||
|
|
||||||
|
def test_async_warning(self):
|
||||||
|
f = asyncio.Future(loop=self.loop)
|
||||||
|
with self.assertWarnsRegex(RuntimeWarning,
|
||||||
|
'function is deprecated, use ensure_'):
|
||||||
|
self.assertIs(f, asyncio.async(f))
|
||||||
|
|
||||||
def test_task_repr(self):
|
def test_task_repr(self):
|
||||||
self.loop.set_debug(False)
|
self.loop.set_debug(False)
|
||||||
|
@ -1420,7 +1426,7 @@ class TaskTests(test_utils.TestCase):
|
||||||
else:
|
else:
|
||||||
proof += 10
|
proof += 10
|
||||||
|
|
||||||
f = asyncio.async(outer(), loop=self.loop)
|
f = asyncio.ensure_future(outer(), loop=self.loop)
|
||||||
test_utils.run_briefly(self.loop)
|
test_utils.run_briefly(self.loop)
|
||||||
f.cancel()
|
f.cancel()
|
||||||
self.loop.run_until_complete(f)
|
self.loop.run_until_complete(f)
|
||||||
|
@ -1445,7 +1451,7 @@ class TaskTests(test_utils.TestCase):
|
||||||
d, p = yield from asyncio.wait([inner()], loop=self.loop)
|
d, p = yield from asyncio.wait([inner()], loop=self.loop)
|
||||||
proof += 100
|
proof += 100
|
||||||
|
|
||||||
f = asyncio.async(outer(), loop=self.loop)
|
f = asyncio.ensure_future(outer(), loop=self.loop)
|
||||||
test_utils.run_briefly(self.loop)
|
test_utils.run_briefly(self.loop)
|
||||||
f.cancel()
|
f.cancel()
|
||||||
self.assertRaises(
|
self.assertRaises(
|
||||||
|
@ -1501,7 +1507,7 @@ class TaskTests(test_utils.TestCase):
|
||||||
yield from asyncio.shield(inner(), loop=self.loop)
|
yield from asyncio.shield(inner(), loop=self.loop)
|
||||||
proof += 100
|
proof += 100
|
||||||
|
|
||||||
f = asyncio.async(outer(), loop=self.loop)
|
f = asyncio.ensure_future(outer(), loop=self.loop)
|
||||||
test_utils.run_briefly(self.loop)
|
test_utils.run_briefly(self.loop)
|
||||||
f.cancel()
|
f.cancel()
|
||||||
with self.assertRaises(asyncio.CancelledError):
|
with self.assertRaises(asyncio.CancelledError):
|
||||||
|
@ -1668,7 +1674,7 @@ class TaskTests(test_utils.TestCase):
|
||||||
|
|
||||||
# schedule the task
|
# schedule the task
|
||||||
coro = kill_me(self.loop)
|
coro = kill_me(self.loop)
|
||||||
task = asyncio.async(coro, loop=self.loop)
|
task = asyncio.ensure_future(coro, loop=self.loop)
|
||||||
self.assertEqual(asyncio.Task.all_tasks(loop=self.loop), {task})
|
self.assertEqual(asyncio.Task.all_tasks(loop=self.loop), {task})
|
||||||
|
|
||||||
# execute the task so it waits for future
|
# execute the task so it waits for future
|
||||||
|
@ -1996,8 +2002,8 @@ class CoroutineGatherTests(GatherTestsBase, test_utils.TestCase):
|
||||||
yield from waiter
|
yield from waiter
|
||||||
proof += 1
|
proof += 1
|
||||||
|
|
||||||
child1 = asyncio.async(inner(), loop=self.one_loop)
|
child1 = asyncio.ensure_future(inner(), loop=self.one_loop)
|
||||||
child2 = asyncio.async(inner(), loop=self.one_loop)
|
child2 = asyncio.ensure_future(inner(), loop=self.one_loop)
|
||||||
gatherer = None
|
gatherer = None
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
|
@ -2007,7 +2013,7 @@ class CoroutineGatherTests(GatherTestsBase, test_utils.TestCase):
|
||||||
yield from gatherer
|
yield from gatherer
|
||||||
proof += 100
|
proof += 100
|
||||||
|
|
||||||
f = asyncio.async(outer(), loop=self.one_loop)
|
f = asyncio.ensure_future(outer(), loop=self.one_loop)
|
||||||
test_utils.run_briefly(self.one_loop)
|
test_utils.run_briefly(self.one_loop)
|
||||||
self.assertTrue(f.cancel())
|
self.assertTrue(f.cancel())
|
||||||
with self.assertRaises(asyncio.CancelledError):
|
with self.assertRaises(asyncio.CancelledError):
|
||||||
|
@ -2034,7 +2040,7 @@ class CoroutineGatherTests(GatherTestsBase, test_utils.TestCase):
|
||||||
def outer():
|
def outer():
|
||||||
yield from asyncio.gather(inner(a), inner(b), loop=self.one_loop)
|
yield from asyncio.gather(inner(a), inner(b), loop=self.one_loop)
|
||||||
|
|
||||||
f = asyncio.async(outer(), loop=self.one_loop)
|
f = asyncio.ensure_future(outer(), loop=self.one_loop)
|
||||||
test_utils.run_briefly(self.one_loop)
|
test_utils.run_briefly(self.one_loop)
|
||||||
a.set_result(None)
|
a.set_result(None)
|
||||||
test_utils.run_briefly(self.one_loop)
|
test_utils.run_briefly(self.one_loop)
|
||||||
|
|
|
@ -37,7 +37,7 @@ class ProactorTests(test_utils.TestCase):
|
||||||
def test_close(self):
|
def test_close(self):
|
||||||
a, b = self.loop._socketpair()
|
a, b = self.loop._socketpair()
|
||||||
trans = self.loop._make_socket_transport(a, asyncio.Protocol())
|
trans = self.loop._make_socket_transport(a, asyncio.Protocol())
|
||||||
f = asyncio.async(self.loop.sock_recv(b, 100))
|
f = asyncio.ensure_future(self.loop.sock_recv(b, 100))
|
||||||
trans.close()
|
trans.close()
|
||||||
self.loop.run_until_complete(f)
|
self.loop.run_until_complete(f)
|
||||||
self.assertEqual(f.result(), b'')
|
self.assertEqual(f.result(), b'')
|
||||||
|
|
Loading…
Reference in New Issue