MCPcopy
hub / github.com/MagicStack/asyncpg / close

Method close

asyncpg/pool.py:939–982  ·  view source on GitHub ↗

Attempt to gracefully close all connections in the pool. Wait until all pool connections are released, close them and shut down the pool. If any error (including cancellation) occurs in ``close()`` the pool will terminate by calling :meth:`Pool.terminate() <pool.Poo

(self)

Source from the content-addressed store, hash-verified

937 return await asyncio.shield(ch.release(timeout))
938
939 async def close(self):
940 """Attempt to gracefully close all connections in the pool.
941
942 Wait until all pool connections are released, close them and
943 shut down the pool. If any error (including cancellation) occurs
944 in ``close()`` the pool will terminate by calling
945 :meth:`Pool.terminate() <pool.Pool.terminate>`.
946
947 It is advisable to use :func:`python:asyncio.wait_for` to set
948 a timeout.
949
950 .. versionchanged:: 0.16.0
951 ``close()`` now waits until all pool connections are released
952 before closing them and the pool. Errors raised in ``close()``
953 will cause immediate pool termination.
954 """
955 if self._closed:
956 return
957 self._check_init()
958
959 self._closing = True
960
961 warning_callback = None
962 try:
963 warning_callback = self._loop.call_later(
964 60, self._warn_on_long_close)
965
966 release_coros = [
967 ch.wait_until_released() for ch in self._holders]
968 await asyncio.gather(*release_coros)
969
970 close_coros = [
971 ch.close() for ch in self._holders]
972 await asyncio.gather(*close_coros)
973
974 except (Exception, asyncio.CancelledError):
975 self.terminate()
976 raise
977
978 finally:
979 if warning_callback is not None:
980 warning_callback.cancel()
981 self._closed = True
982 self._closing = False
983
984 def _warn_on_long_close(self):
985 logger.warning('Pool.close() is taking over 60 seconds to complete. '

Calls 3

_check_initMethod · 0.95
terminateMethod · 0.95
wait_until_releasedMethod · 0.80