MCPcopy
hub / github.com/psycopg/psycopg / close

Method close

psycopg_pool/psycopg_pool/pool.py:427–466  ·  view source on GitHub ↗

Close the pool and make it unavailable to new clients. All the waiting and future clients will fail to acquire a connection with a `PoolClosed` exception. Currently used connections will not be closed until returned to the pool. Wait *timeout* seconds for threads to

(self, timeout: float = 5.0)

Source from the content-addressed store, hash-verified

425 self.run_task(Schedule(self, ShrinkPool(self), self.max_idle))
426
427 def close(self, timeout: float = 5.0) -> None:
428 """Close the pool and make it unavailable to new clients.
429
430 All the waiting and future clients will fail to acquire a connection
431 with a `PoolClosed` exception. Currently used connections will not be
432 closed until returned to the pool.
433
434 Wait *timeout* seconds for threads to terminate their job, if positive.
435 If the timeout expires the pool is closed anyway, although it may raise
436 some warnings on exit.
437 """
438 if self._closed:
439 return
440
441 with self._lock:
442 self._closed = True
443 logger.debug("pool %r closed", self.name)
444
445 # Take waiting client and pool connections out of the state
446 waiting = list(self._waiting)
447 self._waiting.clear()
448 connections = list(self._pool)
449 self._pool.clear()
450
451 # Take the workers out of the pool. Will stop them outside the lock
452 workers = self._signal_stop_worker()
453
454 # Now that the flag _closed is set, getconn will fail immediately,
455 # putconn will just close the returned connection.
456
457 # Wait for the worker tasks to terminate
458 gather(*workers, timeout=timeout)
459
460 # Close the connections that were still in the pool
461 for conn in connections:
462 self._close_connection(conn)
463
464 # Signal to eventual clients in the queue that business is closed.
465 for pos in waiting:
466 pos.fail(PoolClosed(f"the pool {self.name!r} is closed"))
467
468 def _signal_stop_worker(self) -> list[Worker]:
469 # Stop the scheduler

Callers 8

waitMethod · 0.95
__exit__Method · 0.95
test_open_waitFunction · 0.95
waitMethod · 0.45
waitMethod · 0.45
_close_connectionMethod · 0.45

Calls 6

_signal_stop_workerMethod · 0.95
_close_connectionMethod · 0.95
PoolClosedClass · 0.85
gatherFunction · 0.70
clearMethod · 0.45
failMethod · 0.45

Tested by 3

test_open_waitFunction · 0.76