Skip to content

Commit 59eb9a4

Browse files
author
Yury Selivanov
committed
asyncio: async() function is deprecated in favour of ensure_future().
1 parent 740169c commit 59eb9a4

7 files changed

Lines changed: 57 additions & 34 deletions

File tree

Lib/asyncio/base_events.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ def run_until_complete(self, future):
315315
self._check_closed()
316316

317317
new_task = not isinstance(future, futures.Future)
318-
future = tasks.async(future, loop=self)
318+
future = tasks.ensure_future(future, loop=self)
319319
if new_task:
320320
# An exception is raised if the future didn't complete, so there
321321
# is no need to log the "destroy pending task" message

Lib/asyncio/tasks.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
__all__ = ['Task',
44
'FIRST_COMPLETED', 'FIRST_EXCEPTION', 'ALL_COMPLETED',
55
'wait', 'wait_for', 'as_completed', 'sleep', 'async',
6-
'gather', 'shield',
6+
'gather', 'shield', 'ensure_future',
77
]
88

99
import concurrent.futures
@@ -12,6 +12,7 @@
1212
import linecache
1313
import sys
1414
import traceback
15+
import warnings
1516
import weakref
1617

1718
from . import coroutines
@@ -327,7 +328,7 @@ def wait(fs, *, loop=None, timeout=None, return_when=ALL_COMPLETED):
327328
if loop is None:
328329
loop = events.get_event_loop()
329330

330-
fs = {async(f, loop=loop) for f in set(fs)}
331+
fs = {ensure_future(f, loop=loop) for f in set(fs)}
331332

332333
return (yield from _wait(fs, timeout, return_when, loop))
333334

@@ -361,7 +362,7 @@ def wait_for(fut, timeout, *, loop=None):
361362
timeout_handle = loop.call_later(timeout, _release_waiter, waiter)
362363
cb = functools.partial(_release_waiter, waiter)
363364

364-
fut = async(fut, loop=loop)
365+
fut = ensure_future(fut, loop=loop)
365366
fut.add_done_callback(cb)
366367

367368
try:
@@ -449,7 +450,7 @@ def as_completed(fs, *, loop=None, timeout=None):
449450
if isinstance(fs, futures.Future) or coroutines.iscoroutine(fs):
450451
raise TypeError("expect a list of futures, not %s" % type(fs).__name__)
451452
loop = loop if loop is not None else events.get_event_loop()
452-
todo = {async(f, loop=loop) for f in set(fs)}
453+
todo = {ensure_future(f, loop=loop) for f in set(fs)}
453454
from .queues import Queue # Import here to avoid circular import problem.
454455
done = Queue(loop=loop)
455456
timeout_handle = None
@@ -499,6 +500,20 @@ def sleep(delay, result=None, *, loop=None):
499500
def async(coro_or_future, *, loop=None):
500501
"""Wrap a coroutine in a future.
501502
503+
If the argument is a Future, it is returned directly.
504+
505+
This function is deprecated in 3.5. Use asyncio.ensure_future() instead.
506+
"""
507+
508+
warnings.warn("asyncio.async() function is deprecated, use ensure_future()",
509+
DeprecationWarning)
510+
511+
return ensure_future(coro_or_future, loop=loop)
512+
513+
514+
def ensure_future(coro_or_future, *, loop=None):
515+
"""Wrap a coroutine in a future.
516+
502517
If the argument is a Future, it is returned directly.
503518
"""
504519
if isinstance(coro_or_future, futures.Future):
@@ -564,7 +579,7 @@ def gather(*coros_or_futures, loop=None, return_exceptions=False):
564579
arg_to_fut = {}
565580
for arg in set(coros_or_futures):
566581
if not isinstance(arg, futures.Future):
567-
fut = async(arg, loop=loop)
582+
fut = ensure_future(arg, loop=loop)
568583
if loop is None:
569584
loop = fut._loop
570585
# The caller cannot control this future, the "destroy pending task"
@@ -640,7 +655,7 @@ def shield(arg, *, loop=None):
640655
except CancelledError:
641656
res = None
642657
"""
643-
inner = async(arg, loop=loop)
658+
inner = ensure_future(arg, loop=loop)
644659
if inner.done():
645660
# Shortcut.
646661
return inner

Lib/asyncio/windows_events.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ def accept_coro(future, conn):
488488

489489
future = self._register(ov, listener, finish_accept)
490490
coro = accept_coro(future, conn)
491-
tasks.async(coro, loop=self._loop)
491+
tasks.ensure_future(coro, loop=self._loop)
492492
return future
493493

494494
def connect(self, conn, address):

Lib/test/test_asyncio/test_base_events.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ def zero_error_coro():
504504

505505
# Test Future.__del__
506506
with mock.patch('asyncio.base_events.logger') as log:
507-
fut = asyncio.async(zero_error_coro(), loop=self.loop)
507+
fut = asyncio.ensure_future(zero_error_coro(), loop=self.loop)
508508
fut.add_done_callback(lambda *args: self.loop.stop())
509509
self.loop.run_forever()
510510
fut = None # Trigger Future.__del__ or futures._TracebackLogger
@@ -703,7 +703,7 @@ def create_task(self, coro):
703703
self.set_event_loop(loop)
704704

705705
coro = test()
706-
task = asyncio.async(coro, loop=loop)
706+
task = asyncio.ensure_future(coro, loop=loop)
707707
self.assertIsInstance(task, MyTask)
708708

709709
# make warnings quiet
@@ -1265,7 +1265,7 @@ def stop_loop_coro(loop):
12651265
"took .* seconds$")
12661266

12671267
# slow task
1268-
asyncio.async(stop_loop_coro(self.loop), loop=self.loop)
1268+
asyncio.ensure_future(stop_loop_coro(self.loop), loop=self.loop)
12691269
self.loop.run_forever()
12701270
fmt, *args = m_logger.warning.call_args[0]
12711271
self.assertRegex(fmt % tuple(args),

Lib/test/test_asyncio/test_tasks.py

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -92,28 +92,28 @@ def notmuch():
9292
loop.run_until_complete(t)
9393
loop.close()
9494

95-
def test_async_coroutine(self):
95+
def test_ensure_future_coroutine(self):
9696
@asyncio.coroutine
9797
def notmuch():
9898
return 'ok'
99-
t = asyncio.async(notmuch(), loop=self.loop)
99+
t = asyncio.ensure_future(notmuch(), loop=self.loop)
100100
self.loop.run_until_complete(t)
101101
self.assertTrue(t.done())
102102
self.assertEqual(t.result(), 'ok')
103103
self.assertIs(t._loop, self.loop)
104104

105105
loop = asyncio.new_event_loop()
106106
self.set_event_loop(loop)
107-
t = asyncio.async(notmuch(), loop=loop)
107+
t = asyncio.ensure_future(notmuch(), loop=loop)
108108
self.assertIs(t._loop, loop)
109109
loop.run_until_complete(t)
110110
loop.close()
111111

112-
def test_async_future(self):
112+
def test_ensure_future_future(self):
113113
f_orig = asyncio.Future(loop=self.loop)
114114
f_orig.set_result('ko')
115115

116-
f = asyncio.async(f_orig)
116+
f = asyncio.ensure_future(f_orig)
117117
self.loop.run_until_complete(f)
118118
self.assertTrue(f.done())
119119
self.assertEqual(f.result(), 'ko')
@@ -123,19 +123,19 @@ def test_async_future(self):
123123
self.set_event_loop(loop)
124124

125125
with self.assertRaises(ValueError):
126-
f = asyncio.async(f_orig, loop=loop)
126+
f = asyncio.ensure_future(f_orig, loop=loop)
127127

128128
loop.close()
129129

130-
f = asyncio.async(f_orig, loop=self.loop)
130+
f = asyncio.ensure_future(f_orig, loop=self.loop)
131131
self.assertIs(f, f_orig)
132132

133-
def test_async_task(self):
133+
def test_ensure_future_task(self):
134134
@asyncio.coroutine
135135
def notmuch():
136136
return 'ok'
137137
t_orig = asyncio.Task(notmuch(), loop=self.loop)
138-
t = asyncio.async(t_orig)
138+
t = asyncio.ensure_future(t_orig)
139139
self.loop.run_until_complete(t)
140140
self.assertTrue(t.done())
141141
self.assertEqual(t.result(), 'ok')
@@ -145,16 +145,22 @@ def notmuch():
145145
self.set_event_loop(loop)
146146

147147
with self.assertRaises(ValueError):
148-
t = asyncio.async(t_orig, loop=loop)
148+
t = asyncio.ensure_future(t_orig, loop=loop)
149149

150150
loop.close()
151151

152-
t = asyncio.async(t_orig, loop=self.loop)
152+
t = asyncio.ensure_future(t_orig, loop=self.loop)
153153
self.assertIs(t, t_orig)
154154

155-
def test_async_neither(self):
155+
def test_ensure_future_neither(self):
156156
with self.assertRaises(TypeError):
157-
asyncio.async('ok')
157+
asyncio.ensure_future('ok')
158+
159+
def test_async_warning(self):
160+
f = asyncio.Future(loop=self.loop)
161+
with self.assertWarnsRegex(DeprecationWarning,
162+
'function is deprecated, use ensure_'):
163+
self.assertIs(f, asyncio.async(f))
158164

159165
def test_task_repr(self):
160166
self.loop.set_debug(False)
@@ -1420,7 +1426,7 @@ def outer():
14201426
else:
14211427
proof += 10
14221428

1423-
f = asyncio.async(outer(), loop=self.loop)
1429+
f = asyncio.ensure_future(outer(), loop=self.loop)
14241430
test_utils.run_briefly(self.loop)
14251431
f.cancel()
14261432
self.loop.run_until_complete(f)
@@ -1445,7 +1451,7 @@ def outer():
14451451
d, p = yield from asyncio.wait([inner()], loop=self.loop)
14461452
proof += 100
14471453

1448-
f = asyncio.async(outer(), loop=self.loop)
1454+
f = asyncio.ensure_future(outer(), loop=self.loop)
14491455
test_utils.run_briefly(self.loop)
14501456
f.cancel()
14511457
self.assertRaises(
@@ -1501,7 +1507,7 @@ def outer():
15011507
yield from asyncio.shield(inner(), loop=self.loop)
15021508
proof += 100
15031509

1504-
f = asyncio.async(outer(), loop=self.loop)
1510+
f = asyncio.ensure_future(outer(), loop=self.loop)
15051511
test_utils.run_briefly(self.loop)
15061512
f.cancel()
15071513
with self.assertRaises(asyncio.CancelledError):
@@ -1668,7 +1674,7 @@ def kill_me(loop):
16681674

16691675
# schedule the task
16701676
coro = kill_me(self.loop)
1671-
task = asyncio.async(coro, loop=self.loop)
1677+
task = asyncio.ensure_future(coro, loop=self.loop)
16721678
self.assertEqual(asyncio.Task.all_tasks(loop=self.loop), {task})
16731679

16741680
# execute the task so it waits for future
@@ -1996,8 +2002,8 @@ def inner():
19962002
yield from waiter
19972003
proof += 1
19982004

1999-
child1 = asyncio.async(inner(), loop=self.one_loop)
2000-
child2 = asyncio.async(inner(), loop=self.one_loop)
2005+
child1 = asyncio.ensure_future(inner(), loop=self.one_loop)
2006+
child2 = asyncio.ensure_future(inner(), loop=self.one_loop)
20012007
gatherer = None
20022008

20032009
@asyncio.coroutine
@@ -2007,7 +2013,7 @@ def outer():
20072013
yield from gatherer
20082014
proof += 100
20092015

2010-
f = asyncio.async(outer(), loop=self.one_loop)
2016+
f = asyncio.ensure_future(outer(), loop=self.one_loop)
20112017
test_utils.run_briefly(self.one_loop)
20122018
self.assertTrue(f.cancel())
20132019
with self.assertRaises(asyncio.CancelledError):
@@ -2034,7 +2040,7 @@ def inner(f):
20342040
def outer():
20352041
yield from asyncio.gather(inner(a), inner(b), loop=self.one_loop)
20362042

2037-
f = asyncio.async(outer(), loop=self.one_loop)
2043+
f = asyncio.ensure_future(outer(), loop=self.one_loop)
20382044
test_utils.run_briefly(self.one_loop)
20392045
a.set_result(None)
20402046
test_utils.run_briefly(self.one_loop)

Lib/test/test_asyncio/test_windows_events.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def setUp(self):
3737
def test_close(self):
3838
a, b = self.loop._socketpair()
3939
trans = self.loop._make_socket_transport(a, asyncio.Protocol())
40-
f = asyncio.async(self.loop.sock_recv(b, 100))
40+
f = asyncio.ensure_future(self.loop.sock_recv(b, 100))
4141
trans.close()
4242
self.loop.run_until_complete(f)
4343
self.assertEqual(f.result(), b'')

Misc/NEWS

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ Core and Builtins
4242

4343
- Issue #21354: PyCFunction_New function is exposed by python DLL again.
4444

45-
- asyncio: New event loop APIs: set_task_factory() and get_task_factory()
45+
- asyncio: New event loop APIs: set_task_factory() and get_task_factory().
46+
47+
- asyncio: async() function is deprecated in favour of ensure_future().
4648

4749
Library
4850
-------

0 commit comments

Comments
 (0)