Skip to content

psycopg/psycogreen

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

psycogreen -- psycopg2 integration with coroutine libraries

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.

What about Psycopg 3?

The Psycopg 3 module doesn't require psycogreen; psycogreen has no effect on Psycopg 3.

In Psycopg 3:

  • gevent is supported automatically from 3.1.14 on. Please avoid to use older versions.
  • Eventlet is currently not supported, as the project doesn't seem very active anymore. Support can be added if requested.
  • uWsgi is currently in maintenance mode, so Psycopg 3 support doesn't seem necessary.

Installation

pip install psycogreen

Module psycogreen.eventlet

Eventlet currently supports `psycopg2` out-of-the-box andpsycogreenis not necessary. See `the documentation`__ for patching instruction. If for any reason you want to avoid using Eventlet monkeypatching you can usepsycogreen.eventlet.patch_psycopg(). Functionpsycogreen.eventlet.patch_psycopg()Enable async processing in Psycopg integrated with the Eventlet events loop. It is performed by registeringeventlet_wait_callback()as psycopg2 wait callback. Functionpsycogreen.eventlet.eventlet_wait_callback(conn)A wait callback integrating with Eventlet events loop. An example script showing concurrent usage ofpsycopg2withurlopen()with Eventlet is available in |tests/test_eventlet.py|__. Modulepsycogreen.gevent---------------------------- In order to use psycopg2 asynchronously with `gevent`_ you can usepsycogreen.gevent.patch_psycopg(). Functionpsycogreen.gevent.patch_psycopg()Enable async processing in Psycopg integrated with the gevent events loop. It is performed by registeringgevent_wait_callback()as psycopg2 wait callback. Functionpsycogreen.gevent.gevent_wait_callback(conn)A wait callback integrating with gevent events loop. An example script showing concurrent usage ofpsycopg2withurlopen()`` 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.