Skip to main content

A multithread-safe resource pool based on synchronized queue

Project description

The main problem with psycopg2.pool (https://github.com/psycopg/psycopg2/blob/master/lib/pool.py), for example, is that the pool raises an exception (instead of blocking) when there are no more connections in the pool, and you either have to match the number of connections to the number of workers, or implement retry logic. Also, it doesn’t implement connection recycling (on timeout or usage count), and therefore, doesn’t fully address issue with stale connections and suited less (scales worse) for large production installations.

This implementation is based on synchronized queue (https://docs.python.org/3/library/queue.html), and thus multithred safe. This is a streamlined port from Java version that was implemented about ten years ago and that has since then been running in heavy production evironment of one of our financial clients.

This implementation features:

  • A pool of generic resources that can be extended for specific resources like psycopg2 connections. Psycopg2 connection pool implementation is provided.

  • On-demand lazy resource opening.

  • Idle and open timeout recycling. Requires user code to execute pool.recycle() method periodically (or start recycler thread by pool.startRecycler()), for example, once a minute. If this method isn’t executed periodically, then the recycling is performed only when the resource are either taken or returned back to the pool, and therefore, the pool can accumulate a number of idle connections that exceed the idle or open timeouts.

  • Usage count recycling.

  • Recycling on exception.

  • Recycling on a resource status.

  • Context manager allows to use the pool with “with” context manager so that the resources could be returned safely to the pool.

  • LIFO queue helps the pool keep number of open resources to the minimum.

This pool can be utilized successfully in large production installations as it tries to keep the number of open resources to the minimum, yet providing sufficient number of “hot” (open) resources to avoid open/close cost.

License

OSI Approved 3 clause BSD License

Prerequisites

  • Python 3.7+ (with queue)

  • For psycopg2 connections: psycopg2 2.8.2+

Installation

If prerequisites are met, you can install queuepool like any other Python package, using pip to download it from PyPI:

$ pip install queuepool

or using setup.py if you have downloaded the source package locally:

$ python setup.py build $ sudo python setup.py install

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

queuepool-1.3.0.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

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

queuepool-1.3.0-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file queuepool-1.3.0.tar.gz.

File metadata

  • Download URL: queuepool-1.3.0.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for queuepool-1.3.0.tar.gz
Algorithm Hash digest
SHA256 cb468f8e3af230448a2d0f4501e5c79ca797fc2dd2d3e5b14462312f9c9f8dc4
MD5 5f1238e0e95b890ced65263a0f77a6d1
BLAKE2b-256 5d2202dd30c699d65b96a19227804c09899af4d26ad2bfec00eaeb677c46c6c0

See more details on using hashes here.

File details

Details for the file queuepool-1.3.0-py3-none-any.whl.

File metadata

  • Download URL: queuepool-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 12.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for queuepool-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 107a83486eab3b3c4066b74d64a27f852eee3456221f64163d082eecf58fd8a7
MD5 be5478f8b12a216ebcce5a32499ddba8
BLAKE2b-256 b6b08f99823ef2f2ea9d8f7e9a5ee7dd91362e4eec1a21fc439a9c99910b9904

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