Skip to main content

psycopg2 integration with coroutine libraries

Project description

The psycogreen package enables psycopg2 to work with coroutine libraries, using asynchronous calls internally but offering a blocking interface so that regular code can run unmodified.

Psycopg offers coroutines support since release 2.2. Because the main module is a C extension it cannot be monkey-patched to become coroutine-friendly. Instead it exposes a hook that coroutine libraries can use to install a function integrating with their event scheduler. Psycopg will call the function whenever it executes a libpq call that may block. psycogreen is a collection of “wait callbacks” useful to integrate Psycopg with different coroutine libraries.

Installation

pip install psycogreen

Module psycogreen.eventlet

Eventlet currently supports Psycopg out-of-the-box and psycogreen is not necessary. See the documentation for patching instruction.

If for any reason you want to avoid using Eventlet monkeypatching you can use psycogreen.eventlet.patch_psycopg().

Function psycogreen.eventlet.patch_psycopg()

Enable async processing in Psycopg integrated with the Eventlet events loop. It is performed by registering eventlet_wait_callback() as psycopg2 wait callback.

Function psycogreen.eventlet.eventlet_wait_callback(conn)

A wait callback integrating with Eventlet events loop.

An example script showing concurrent usage of psycopg2 with urlopen() with Eventlet is available in tests/test_eventlet.py.

Module psycogreen.gevent

In order to use psycopg2 asynchronously with gevent you can use psycogreen.gevent.patch_psycopg().

Function psycogreen.gevent.patch_psycopg()

Enable async processing in Psycopg integrated with the gevent events loop. It is performed by registering gevent_wait_callback() as psycopg2 wait callback.

Function psycogreen.gevent.gevent_wait_callback(conn)

A wait callback integrating with gevent events loop.

An example script showing concurrent usage of psycopg2 with urlopen() with gevent is available in tests/test_gevent.py.

uWSGI green threads

Roberto De Ioris is writing uGreen, a green thread implementation on top of the uWSGI async platform.

He has performed some tests using both psycopg2 async support and psycopg2 green support and has reported no problem in their stress tests with both the async styles.

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

psycogreen-1.0.2.tar.gz (5.4 kB view details)

Uploaded Source

File details

Details for the file psycogreen-1.0.2.tar.gz.

File metadata

  • Download URL: psycogreen-1.0.2.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.6.9

File hashes

Hashes for psycogreen-1.0.2.tar.gz
Algorithm Hash digest
SHA256 c429845a8a49cf2f76b71265008760bcd7c7c77d80b806db4dc81116dbcd130d
MD5 4f6f68ba5ffb1d6908160de3dacd3618
BLAKE2b-256 eb724a7965cf54e341006ad74cdc72cd6572c789bc4f4e3fadc78672f1fbcfbd

See more details on using hashes here.

Supported by

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