@@ -81,12 +81,11 @@ is called.
8181If you wait for a future, you should check early if the future was cancelled to
8282avoid useless operations. Example::
8383
84- @coroutine
85- def slow_operation(fut):
84+ async def slow_operation(fut):
8685 if fut.cancelled():
8786 return
8887 # ... slow computation ...
89- yield from fut
88+ await fut
9089 # ...
9190
9291The :func: `shield ` function can also be used to ignore cancellation.
@@ -99,7 +98,7 @@ Concurrency and multithreading
9998
10099An event loop runs in a thread and executes all callbacks and tasks in the same
101100thread. While a task is running in the event loop, no other task is running in
102- the same thread. But when the task uses ``yield from ``, the task is suspended
101+ the same thread. But when the task uses ``await ``, the task is suspended
103102and the event loop executes the next task.
104103
105104To schedule a callback from a different thread, the
@@ -192,8 +191,7 @@ Example with the bug::
192191
193192 import asyncio
194193
195- @asyncio.coroutine
196- def test():
194+ async def test():
197195 print("never scheduled")
198196
199197 test()
@@ -270,10 +268,9 @@ traceback where the task was created. Output in debug mode::
270268There are different options to fix this issue. The first option is to chain the
271269coroutine in another coroutine and use classic try/except::
272270
273- @asyncio.coroutine
274- def handle_exception():
271+ async def handle_exception():
275272 try:
276- yield from bug()
273+ await bug()
277274 except Exception:
278275 print("exception consumed")
279276
@@ -300,34 +297,30 @@ Chain coroutines correctly
300297--------------------------
301298
302299When a coroutine function calls other coroutine functions and tasks, they
303- should be chained explicitly with ``yield from ``. Otherwise, the execution is
300+ should be chained explicitly with ``await ``. Otherwise, the execution is
304301not guaranteed to be sequential.
305302
306303Example with different bugs using :func: `asyncio.sleep ` to simulate slow
307304operations::
308305
309306 import asyncio
310307
311- @asyncio.coroutine
312- def create():
313- yield from asyncio.sleep(3.0)
308+ async def create():
309+ await asyncio.sleep(3.0)
314310 print("(1) create file")
315311
316- @asyncio.coroutine
317- def write():
318- yield from asyncio.sleep(1.0)
312+ async def write():
313+ await asyncio.sleep(1.0)
319314 print("(2) write into file")
320315
321- @asyncio.coroutine
322- def close():
316+ async def close():
323317 print("(3) close file")
324318
325- @asyncio.coroutine
326- def test():
319+ async def test():
327320 asyncio.ensure_future(create())
328321 asyncio.ensure_future(write())
329322 asyncio.ensure_future(close())
330- yield from asyncio.sleep(2.0)
323+ await asyncio.sleep(2.0)
331324 loop.stop()
332325
333326 loop = asyncio.get_event_loop()
@@ -359,24 +352,22 @@ The loop stopped before the ``create()`` finished, ``close()`` has been called
359352before ``write() ``, whereas coroutine functions were called in this order:
360353``create() ``, ``write() ``, ``close() ``.
361354
362- To fix the example, tasks must be marked with ``yield from ``::
355+ To fix the example, tasks must be marked with ``await ``::
363356
364- @asyncio.coroutine
365- def test():
366- yield from asyncio.ensure_future(create())
367- yield from asyncio.ensure_future(write())
368- yield from asyncio.ensure_future(close())
369- yield from asyncio.sleep(2.0)
357+ async def test():
358+ await asyncio.ensure_future(create())
359+ await asyncio.ensure_future(write())
360+ await asyncio.ensure_future(close())
361+ await asyncio.sleep(2.0)
370362 loop.stop()
371363
372364Or without ``asyncio.ensure_future() ``::
373365
374- @asyncio.coroutine
375- def test():
376- yield from create()
377- yield from write()
378- yield from close()
379- yield from asyncio.sleep(2.0)
366+ async def test():
367+ await create()
368+ await write()
369+ await close()
370+ await asyncio.sleep(2.0)
380371 loop.stop()
381372
382373
0 commit comments