psycogreen / README.rst

psycogreen -- integrating psycopg2 with coroutine libraries

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

Since release 2.2, Psycopg offers coroutines support.

Psycopg is a C extension module, so 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.

Module psycogreen.eventlet

Eventlet support Psycopg out-of-the-box: Psycopg can be patched together with the standard library: see the documentation.

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

Function psycogreen.eventlet.eventlet_wait_callback(conn)
A wait callback integrating with Eventlet events loop.
Function psycogreen.eventlet.make_psycopg_green()
Register eventlet_wait_callback() into psycopg2

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

Module psycogreen.gevent

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

Function psycogreen.gevent.gevent_wait_callback(conn)
A wait callback integrating with gevent events loop.
Function psycogreen.gevent.make_psycopg_green()
Register gevent_wait_callback() into psycopg2

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

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.