Skip to main content

Utils to use with psycopg2 such as a connection pool and cursor

Project description

psycopg2 utils

This package contains helper classes for psycopg2.

Installation

pip install psycopg2-utils

ConnectionPool

This connection pool is thread safe and does not raise an exception when the pool is empty and a thread tried to get a connection from it. Instead the thread will wait for a connection to become available in the pool. Connections will also not be closed when returned to the pool until they have been idle for some specified time.

from psycopg2_utils import ConnectionPool

config = {
    "minconn": os.environ["DB_MINCONN"],
    "maxconn": os.environ["DB_MAXCONN"],
    "idle_time": os.environ["IDLE_TIME"],
    "dbname": os.environ["DB_NAME"],
    "host": os.environ["DB_HOST"],
    "port": os.environ["DB_PORT"],
    "user": os.environ["DB_USER"],
    "password": os.environ["DB_PASS"],
    "options": "-c search_path={}".format(os.environ["DB_SCHEMA"]),
}
pool = ConnectionPool(**config)

# Get connection from the pool
con = pool.getconn()

# Return connection to the pool
pool.putconn(con)

Cursor

This cursor extends the dict curser and enables logging the sql queries when the log level is set to DEBUG and also logs how long each query takes to execute if log level is INFO and there is an environment variable named METRIC_LOGGING that has the value "TRUE".

from psycopg2 import connect
from psycopg2_utils import Cursor

con = connect("")
cur = con.cursor(cursor_factory=Cursor)

pooled_cursor

This allows a method of a class to be decorated so that it will automatically supply the method with a cursor to use. It requires that the class has a property named pool containing a connection pool that implements the AbstractConnectionPool.

from psycopg2_utils import pooled_cursor

class MyClass:
    def __init__(self, config):
        self.pool = ConnectionPool(**config)

    @pooled_cursor
    def get_count(self, cursor):
        cursor.execute("SELECT count(*) FROM table;")
        return cursor.fetchone()[0]

my_class = MyClass(config)

#The method can then be used like this:
count = my_class.get_count()

# If you already hold a cursor and don't want to get a new one
# from the pool then it can be passed into the method.
cursor = some_method_that_gets_cursor()
count = my_class.get_count(cursor=cursor)

The wrapper will take care of getting a connection from the pool and returning it to the pool at the end. It also does a commit when there is no exception.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

psycopg2-utils-1.0.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

psycopg2_utils-1.0-py2.py3-none-any.whl (6.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file psycopg2-utils-1.0.tar.gz.

File metadata

  • Download URL: psycopg2-utils-1.0.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.6

File hashes

Hashes for psycopg2-utils-1.0.tar.gz
Algorithm Hash digest
SHA256 21f529afbc5dc903c09c42e496bc5c3813acec04d2d1355a1ee1f416f96e93e4
MD5 8daa11774d1170d0db268ea856669041
BLAKE2b-256 64dfda82602452a52c6dca009f3e3c6388f579ebea86ab783f4ee1120ab0580a

See more details on using hashes here.

File details

Details for the file psycopg2_utils-1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: psycopg2_utils-1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.6

File hashes

Hashes for psycopg2_utils-1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 2755207c4ca0dab7836a66aba057a12a412697a3f76b23247b5245e5f2f3fd03
MD5 d6bd1a8a64c43b806ce7a251d89852ef
BLAKE2b-256 29ad744f3e77fc1cf3c66db4a1e7e80bbb56975d577288791dd9b48c319a867d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page