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)
| 1055 | return await asyncio.shield(ch.release(timeout)) |
| 1056 | |
| 1057 | async def close(self): |
| 1058 | """Attempt to gracefully close all connections in the pool. |
| 1059 | |
| 1060 | Wait until all pool connections are released, close them and |
| 1061 | shut down the pool. If any error (including cancellation) occurs |
| 1062 | in ``close()`` the pool will terminate by calling |
| 1063 | :meth:`Pool.terminate() <pool.Pool.terminate>`. |
| 1064 | |
| 1065 | It is advisable to use :func:`python:asyncio.wait_for` to set |
| 1066 | a timeout. |
| 1067 | |
| 1068 | .. versionchanged:: 0.16.0 |
| 1069 | ``close()`` now waits until all pool connections are released |
| 1070 | before closing them and the pool. Errors raised in ``close()`` |
| 1071 | will cause immediate pool termination. |
| 1072 | """ |
| 1073 | if self._closed: |
| 1074 | return |
| 1075 | self._check_init() |
| 1076 | |
| 1077 | self._closing = True |
| 1078 | |
| 1079 | warning_callback = None |
| 1080 | try: |
| 1081 | if self._maintenance_task is not None: |
| 1082 | self._maintenance_task.cancel() |
| 1083 | await asyncio.gather( |
| 1084 | self._maintenance_task, return_exceptions=True) |
| 1085 | self._maintenance_task = None |
| 1086 | |
| 1087 | warning_callback = self._loop.call_later( |
| 1088 | 60, self._warn_on_long_close) |
| 1089 | |
| 1090 | release_coros = [ |
| 1091 | ch.wait_until_released() for ch in self._holders] |
| 1092 | await asyncio.gather(*release_coros) |
| 1093 | |
| 1094 | close_coros = [ |
| 1095 | ch.close() for ch in self._holders] |
| 1096 | await asyncio.gather(*close_coros) |
| 1097 | |
| 1098 | except (Exception, asyncio.CancelledError): |
| 1099 | self.terminate() |
| 1100 | raise |
| 1101 | |
| 1102 | finally: |
| 1103 | if warning_callback is not None: |
| 1104 | warning_callback.cancel() |
| 1105 | self._closed = True |
| 1106 | self._closing = False |
| 1107 | |
| 1108 | def _warn_on_long_close(self): |
| 1109 | logger.warning('Pool.close() is taking over 60 seconds to complete. ' |
no test coverage detected