Closes #21921: Fix ResourceWarning in the asyncio examples: close the event

loop at exit. Patch written by Vajrasky Kok (I modified also the "hello world"
example using a coroutine).
This commit is contained in:
Victor Stinner 2014-07-05 15:38:59 +02:00
parent a9acbe82e7
commit 63b21a8ffa
2 changed files with 12 additions and 3 deletions

View file

@ -651,7 +651,10 @@ Print ``Hello World`` every two seconds, using a callback::
loop = asyncio.get_event_loop()
loop.call_soon(print_and_repeat, loop)
loop.run_forever()
try:
loop.run_forever()
finally:
loop.close()
.. seealso::
@ -679,5 +682,8 @@ Register handlers for signals :py:data:`SIGINT` and :py:data:`SIGTERM`::
print("Event loop running forever, press CTRL+c to interrupt.")
print("pid %s: send SIGINT or SIGTERM to exit." % os.getpid())
loop.run_forever()
try:
loop.run_forever()
finally:
loop.close()

View file

@ -89,7 +89,10 @@ Print ``"Hello World"`` every two seconds using a coroutine::
yield from asyncio.sleep(2)
loop = asyncio.get_event_loop()
loop.run_until_complete(greet_every_two_seconds())
try:
loop.run_until_complete(greet_every_two_seconds())
finally:
loop.close()
.. seealso::