.close()
can be used by various implementations of the event loop to free up system resources allocated by the loop (or do something else). If you look at the _UnixSelectorEventLoop
code, which is the (default) IOLoop used on Linux, you will find the following code:
def close(self): super().close() for sig in list(self._signal_handlers): self.remove_signal_handler(sig)
Here, for example, close()
removes the signal handlers registered with loop.add_signal_handler()
.
Since several IOLoops can be run on different threads, or new IOLoops can be created after closing the old one (see asyncio.new_event_loop()
), closing them should be considered a good habit.
Refresh
Starting with Python 3.7, it is recommended to use asyncio.run
instead of run_until_complete()
:
# Python 3.7+ def main(): body = asyncio.run(fetch()) print(body.decode('latin-1'), end='')
Among other things, asyncio.run
will take care of the finally
close()
loop.
source share