@@ -58,7 +58,7 @@ approach rather useless.
5858::
5959
6060 import asyncio
61-
61+
6262 # This creates an event loop and indefinitely cycles through
6363 # its queue of tasks.
6464 event_loop = asyncio.new_event_loop()
@@ -277,32 +277,33 @@ and executes the remaining statements in its body.
277277When a coroutine finishes, it raises a :exc: `StopIteration ` exception with the
278278return value attached in the :attr: `~StopIteration.value ` attribute.
279279
280- ::
280+ .. code-block ::
281+ :linenos:
281282
282- 1 class Rock:
283- 2 def __await__(self):
284- 3 value_sent_in = yield 7
285- 4 print(f"Rock.__await__ resuming with value: {value_sent_in}.")
286- 5 return value_sent_in
287- 6
288- 7 async def main():
289- 8 print("Beginning coroutine main().")
290- 9 rock = Rock()
291- 10 print("Awaiting rock...")
292- 11 value_from_rock = await rock
293- 12 print(f"Coroutine received value: {value_from_rock} from rock.")
294- 13 return 23
295- 14
296- 15 coroutine = main()
297- 16 intermediate_result = coroutine.send(None)
298- 17 print(f"Coroutine paused and returned intermediate value: {intermediate_result}.")
299- 18
300- 19 print(f"Resuming coroutine and sending in value: 42.")
301- 20 try:
302- 21 coroutine.send(42)
303- 22 except StopIteration as e:
304- 23 returned_value = e.value
305- 24 print(f"Coroutine main() finished and provided value: {returned_value}.")
283+ class Rock:
284+ def __await__(self):
285+ value_sent_in = yield 7
286+ print(f"Rock.__await__ resuming with value: {value_sent_in}.")
287+ return value_sent_in
288+
289+ async def main():
290+ print("Beginning coroutine main().")
291+ rock = Rock()
292+ print("Awaiting rock...")
293+ value_from_rock = await rock
294+ print(f"Coroutine received value: {value_from_rock} from rock.")
295+ return 23
296+
297+ coroutine = main()
298+ intermediate_result = coroutine.send(None)
299+ print(f"Coroutine paused and returned intermediate value: {intermediate_result}.")
300+
301+ print(f"Resuming coroutine and sending in value: 42.")
302+ try:
303+ coroutine.send(42)
304+ except StopIteration as e:
305+ returned_value = e.value
306+ print(f"Coroutine main() finished and provided value: {returned_value}.")
306307
307308 That snippet produces this output:
308309
@@ -378,8 +379,8 @@ preventing other tasks from running.
378379::
379380
380381 async def other_work():
381- print(f "I am worker. Work work.")
382-
382+ print("I am worker. Work work.")
383+
383384 async def main():
384385 # Add a few other tasks to the event loop, so there's something
385386 # to do while asynchronously sleeping.
@@ -444,7 +445,7 @@ Note this is also of true of ``asyncio.sleep``.
444445 class YieldToEventLoop:
445446 def __await__(self):
446447 yield
447-
448+
448449 async def _sleep_watcher(future: asyncio.Future, time_to_wake: float):
449450 while True:
450451 if time.time() >= time_to_wake:
0 commit comments