psycogreen
Psycogreen is a library that enables the `psycopg2` PostgreSQL adapter to work with coroutine-based libraries like Gevent and Eventlet. It provides "wait callbacks" that integrate `psycopg2`'s asynchronous calls with the event loops of these coroutine libraries, allowing `psycopg2` to operate in a non-blocking manner within a green-thread environment while presenting a synchronous interface to the application. The library is currently at version 1.0.2, with no new releases since 2020, suggesting it is in maintenance mode.
Warnings
- breaking `psycogreen` is not required and has no effect with `psycopg3`. `psycopg3` (especially from version 3.1.14 onwards) has native Gevent support. Using `psycogreen` with `psycopg3` is unnecessary and may lead to unexpected behavior.
- gotcha `psycopg2` connections are not green thread-safe and cannot be used concurrently by different green threads. Sharing a single connection across multiple greenlets without proper synchronization (e.g., connection pooling or explicit locks) will lead to errors or deadlocks.
- gotcha When `psycogreen` is used, the `connect_timeout` option in `psycopg2.connect()` may not function as expected, potentially causing connections to hang indefinitely during connection attempts. This is a known issue with `psycopg2` in async mode.
- gotcha `COPY` commands (for bulk data transfer) and large objects are currently not supported when a `psycogreen` wait callback is registered. Attempting to use them will result in errors.
- gotcha Eventlet's monkey-patching often provides `psycopg2` compatibility out-of-the-box, making `psycogreen.eventlet.patch_psycopg()` redundant or unnecessary in many cases.
Install
-
pip install psycogreen
Imports
- patch_psycopg (gevent)
from psycogreen.gevent import patch_psycopg
- patch_psycopg (eventlet)
from psycogreen.eventlet import patch_psycopg
Quickstart
import os
import gevent.monkey
import psycopg2
# Apply gevent monkey patching for standard library modules
gevent.monkey.patch_all()
# Apply psycogreen patching for psycopg2
from psycogreen.gevent import patch_psycopg
patch_psycopg()
# Now psycopg2 operations will yield to gevent
try:
# Use os.environ.get for database connection details for runnable example
conn = psycopg2.connect(
host=os.environ.get('PGHOST', 'localhost'),
database=os.environ.get('PGDATABASE', 'testdb'),
user=os.environ.get('PGUSER', 'postgres'),
password=os.environ.get('PGPASSWORD', 'password'),
port=os.environ.get('PGPORT', '5432')
)
cur = conn.cursor()
cur.execute("SELECT 1 + 1 AS result;")
result = cur.fetchone()[0]
print(f"PostgreSQL query result (green): {result}")
cur.close()
conn.close()
except psycopg2.OperationalError as e:
print(f"Could not connect to PostgreSQL. Please ensure a PostgreSQL instance is running and accessible. Error: {e}")
except ImportError as e:
print(f"Required modules (gevent, psycogreen, psycopg2) not installed. Error: {e}")