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.
If for any reason you want to avoid using Eventlet monkeypatching you can use psycogreen.eventlet.patch_psycopg().
An example script showing concurrent usage of psycopg2 with urlopen() with Eventlet is available in tests/test_eventlet.py.
In order to use psycopg2 asynchronously with gevent you can use psycogreen.gevent.patch_psycopg().
An example script showing concurrent usage of psycopg2 with urlopen() with gevent is available in tests/test_gevent.py.