MCPcopy
hub / github.com/psycopg/psycopg / getconn

Method getconn

psycopg_pool/psycopg_pool/pool.py:194–220  ·  view source on GitHub ↗

Obtain a connection from the pool. You should preferably use `connection()`. Use this function only if it is not possible to use the connection as context manager. After using this function you *must* call a corresponding `putconn()`: failing to do so will deplete t

(self, timeout: float | None = None)

Source from the content-addressed store, hash-verified

192 self._stats[self._USAGE_MS] += int(1000.0 * (t1 - t0))
193
194 def getconn(self, timeout: float | None = None) -> CT:
195 """Obtain a connection from the pool.
196
197 You should preferably use `connection()`. Use this function only if
198 it is not possible to use the connection as context manager.
199
200 After using this function you *must* call a corresponding `putconn()`:
201 failing to do so will deplete the pool. A depleted pool is a sad pool:
202 you don't want a depleted pool.
203 """
204 if timeout is None:
205 timeout = self.timeout
206 deadline = monotonic() + timeout
207
208 logger.info("connection requested from %r", self.name)
209 self._stats[self._REQUESTS_NUM] += 1
210
211 self._check_open_getconn()
212
213 try:
214 return self._getconn_with_check_loop(deadline)
215 # Re-raise the timeout exception presenting the user the global
216 # timeout, not the per-attempt one.
217 except PoolTimeout:
218 raise PoolTimeout(
219 f"couldn't get a connection after {timeout:.2f} sec"
220 ) from None
221
222 def _getconn_with_check_loop(self, deadline: float) -> CT:
223 attempt: AttemptWithBackoff | None = None

Callers 15

connectionMethod · 0.95
test_intrans_rollbackFunction · 0.45
test_inerror_rollbackFunction · 0.45
test_active_closeFunction · 0.45
test_fail_rollback_closeFunction · 0.45
test_getconn_checkFunction · 0.45
test_override_closeFunction · 0.45
test_close_returnsFunction · 0.45

Calls 4

_check_open_getconnMethod · 0.95
PoolTimeoutClass · 0.85
infoMethod · 0.45