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.3.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.3-py2.py3-none-any.whl (6.7 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: psycopg2-utils-1.0.3.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.3.tar.gz
Algorithm Hash digest
SHA256 4fd51bd00c927823691aecaa492270a2925324e9ec4b1fb19e9007a9734ee4f4
MD5 f7a0ebe79990426068615e2c6c2d9e96
BLAKE2b-256 b28a6e88e0885f4174056b2dfff06d99c9b209d08f0b29b5d9c6a72d94a4d2ed

See more details on using hashes here.

File details

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

File metadata

  • Download URL: psycopg2_utils-1.0.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.7 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.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 6a71ebbe1e934c1076f92fb0b5b0b6d0b22a5886740694d2d3e11c1c9ec12581
MD5 2ffc2303b0b39575ffdad421debf43b1
BLAKE2b-256 2cae96d5ab1e3068a319c62d56fce2da06803c804baceb7567004d6b9f42eff9

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