Close #20652: asyncio doc: close the event loop in run_forever() example. Fix

also typo. Patch written by Vajrasky Kok.
This commit is contained in:
Victor Stinner 2014-02-17 10:54:30 +01:00
parent 3c1b379ebd
commit 38df2adaeb

View file

@ -229,7 +229,7 @@ Example combining a :class:`Future` and a :ref:`coroutine function
@asyncio.coroutine @asyncio.coroutine
def slow_operation(future): def slow_operation(future):
yield from asyncio.sleep(1) yield from asyncio.sleep(1)
future.set_result('Future in done!') future.set_result('Future is done!')
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
future = asyncio.Future() future = asyncio.Future()
@ -261,7 +261,7 @@ flow::
@asyncio.coroutine @asyncio.coroutine
def slow_operation(future): def slow_operation(future):
yield from asyncio.sleep(1) yield from asyncio.sleep(1)
future.set_result('Future in done!') future.set_result('Future is done!')
def got_result(future): def got_result(future):
print(future.result()) print(future.result())
@ -271,7 +271,10 @@ flow::
future = asyncio.Future() future = asyncio.Future()
asyncio.Task(slow_operation(future)) asyncio.Task(slow_operation(future))
future.add_done_callback(got_result) future.add_done_callback(got_result)
try:
loop.run_forever() loop.run_forever()
finally:
loop.close()
In this example, the future is responsible to display the result and to stop In this example, the future is responsible to display the result and to stop
the loop. the loop.