Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 63b21a8

Browse files
committed
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).
1 parent a9acbe8 commit 63b21a8

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

Doc/library/asyncio-eventloop.rst

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,10 @@ Print ``Hello World`` every two seconds, using a callback::
651651

652652
loop = asyncio.get_event_loop()
653653
loop.call_soon(print_and_repeat, loop)
654-
loop.run_forever()
654+
try:
655+
loop.run_forever()
656+
finally:
657+
loop.close()
655658

656659
.. seealso::
657660

@@ -679,5 +682,8 @@ Register handlers for signals :py:data:`SIGINT` and :py:data:`SIGTERM`::
679682

680683
print("Event loop running forever, press CTRL+c to interrupt.")
681684
print("pid %s: send SIGINT or SIGTERM to exit." % os.getpid())
682-
loop.run_forever()
685+
try:
686+
loop.run_forever()
687+
finally:
688+
loop.close()
683689

Doc/library/asyncio-task.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ Print ``"Hello World"`` every two seconds using a coroutine::
8989
yield from asyncio.sleep(2)
9090

9191
loop = asyncio.get_event_loop()
92-
loop.run_until_complete(greet_every_two_seconds())
92+
try:
93+
loop.run_until_complete(greet_every_two_seconds())
94+
finally:
95+
loop.close()
9396

9497
.. seealso::
9598

0 commit comments

Comments
 (0)