Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update pool methods with docs and elegant syntax #1353

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions lib/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,32 +156,40 @@ class ThreadedConnectionPool(AbstractConnectionPool):
"""A connection pool that works with the threading module."""

def __init__(self, minconn, maxconn, *args, **kwargs):
"""Initialize the threading lock."""
"""Initializes a new instance of ThreadedConnectionPool.

Parameters
----------
minconn : int
The minimum number of connections that should be automatically
created when you initialize the connection pool.
maxconn : int
The maximum number of connections that will be supported by this
connection pool
args : optional, positional arguments
Usually these are passed to the underlying ``connect`` method
of psycopg2
kwargs : optional, keyword only arguments
Usually these are passed to the underlying ``connect`` method
of psycopg2
"""

super().__init__(minconn, maxconn, *args, **kwargs)

import threading
AbstractConnectionPool.__init__(
self, minconn, maxconn, *args, **kwargs)
self._lock = threading.Lock()

def getconn(self, key=None):
"""Get a free connection and assign it to 'key' if not None."""
self._lock.acquire()
try:
with self._lock:
return self._getconn(key)
finally:
self._lock.release()

def putconn(self, conn=None, key=None, close=False):
"""Put away an unused connection."""
self._lock.acquire()
try:
with self._lock:
self._putconn(conn, key, close)
finally:
self._lock.release()

def closeall(self):
"""Close all connections (even the one currently in use.)"""
self._lock.acquire()
try:
with self._lock:
self._closeall()
finally:
self._lock.release()