### Install psycogreen via pip Source: https://context7.com/psycopg/psycogreen/llms.txt Install the package from PyPI to begin using psycogreen. ```bash pip install psycogreen ``` -------------------------------- ### Concurrent Operations with Eventlet Source: https://context7.com/psycopg/psycogreen/llms.txt Uses eventlet.monkey_patch() and psycogreen.eventlet.patch_psycopg() to manage concurrent tasks via a GreenPool. ```python import eventlet eventlet.monkey_patch() import psycogreen.eventlet psycogreen.eventlet.patch_psycopg() from urllib.request import urlopen import psycopg2 import logging logging.basicConfig(level=logging.INFO, format="%(asctime)s %(message)s") logger = logging.getLogger() conn = psycopg2.connect("dbname=postgres") def download(url, request_id): """Fetch URL without blocking other green threads.""" logger.info(f"Download {request_id} start") response = urlopen(url) data = response.read() logger.info(f"Download {request_id} end, received {len(data)} bytes") return data def fetch_from_db(query_id, sleep_seconds): """Query database without blocking other green threads.""" cur = conn.cursor() logger.info(f"Query {query_id} start") cur.execute("SELECT pg_sleep(%s), current_timestamp", (sleep_seconds,)) result = cur.fetchone() logger.info(f"Query {query_id} end") return result # Use GreenPool for concurrent execution pool = eventlet.GreenPool(size=10) # Spawn concurrent operations pool.spawn(download, "http://example.com", 1) pool.spawn(download, "http://example.org", 2) pool.spawn(fetch_from_db, 1, 2) pool.spawn(fetch_from_db, 2, 2) # Wait for all green threads to complete pool.waitall() logger.info("All operations completed") ``` -------------------------------- ### psycogreen.eventlet.eventlet_wait_callback(conn, timeout=-1) Source: https://context7.com/psycopg/psycogreen/llms.txt Low-level wait callback function that integrates psycopg2 with Eventlet's trampoline mechanism. Automatically registered by `patch_psycopg()`, but available for manual registration when needed. ```APIDOC ## psycogreen.eventlet.eventlet_wait_callback(conn, timeout=-1) ### Description Low-level wait callback function that integrates psycopg2 with Eventlet's trampoline mechanism. Automatically registered by `patch_psycopg()`, but available for manual registration when needed. ### Method Python Function Call ### Endpoint N/A ### Parameters - **conn** (psycopg2.extensions.connection) - The database connection object. - **timeout** (int, optional) - The maximum time in seconds to wait for I/O. Defaults to -1 (wait indefinitely). ### Request Example ```python import eventlet eventlet.monkey_patch() from psycopg2 import extensions from psycogreen.eventlet import eventlet_wait_callback # Manually register the wait callback extensions.set_wait_callback(eventlet_wait_callback) import psycopg2 conn = psycopg2.connect("dbname=postgres") cur = conn.cursor() cur.execute("SELECT version()") print(cur.fetchone()) # Output: PostgreSQL version string ``` ### Response N/A (This is a callback function) ### Response Example N/A ``` -------------------------------- ### Concurrent Operations with gevent Source: https://context7.com/psycopg/psycogreen/llms.txt Uses gevent.monkey.patch_all() and psycogreen.gevent.patch_psycopg() to allow concurrent HTTP and database I/O. ```python import gevent import gevent.monkey gevent.monkey.patch_all() import psycogreen.gevent psycogreen.gevent.patch_psycopg() from urllib.request import urlopen import psycopg2 import logging logging.basicConfig(level=logging.INFO, format="%(asctime)s %(message)s") logger = logging.getLogger() conn = psycopg2.connect("dbname=postgres") def download(url, request_id): """Fetch URL without blocking other greenlets.""" logger.info(f"Download {request_id} start") response = urlopen(url) data = response.read() logger.info(f"Download {request_id} end, received {len(data)} bytes") return data def fetch_from_db(query_id, sleep_seconds): """Query database without blocking other greenlets.""" cur = conn.cursor() logger.info(f"Query {query_id} start") cur.execute("SELECT pg_sleep(%s), current_timestamp", (sleep_seconds,)) result = cur.fetchone() logger.info(f"Query {query_id} end") return result # Run HTTP downloads and database queries concurrently jobs = [ gevent.spawn(download, "http://example.com", 1), gevent.spawn(download, "http://example.org", 2), gevent.spawn(fetch_from_db, 1, 2), gevent.spawn(fetch_from_db, 2, 2), ] # All operations interleave - database waits don't block HTTP requests results = gevent.joinall(jobs) for job in jobs: print(f"Result: {job.value}") ``` -------------------------------- ### Register Eventlet wait callback manually Source: https://context7.com/psycopg/psycogreen/llms.txt Low-level registration of the Eventlet wait callback for manual integration. ```python import eventlet eventlet.monkey_patch() from psycopg2 import extensions from psycogreen.eventlet import eventlet_wait_callback # Manually register the wait callback extensions.set_wait_callback(eventlet_wait_callback) import psycopg2 conn = psycopg2.connect("dbname=postgres") cur = conn.cursor() cur.execute("SELECT version()") print(cur.fetchone()) # Output: PostgreSQL version string ``` -------------------------------- ### Patch psycopg2 for Eventlet Source: https://context7.com/psycopg/psycogreen/llms.txt Configures psycopg2 to work with Eventlet. Call after monkey patching and before creating database connections. ```python import eventlet # Monkey patch standard library first eventlet.monkey_patch() # Patch psycopg2 to work with eventlet import psycogreen.eventlet psycogreen.eventlet.patch_psycopg() import psycopg2 conn = psycopg2.connect("dbname=mydb host=localhost user=myuser password=mypass") def query_database(query_id, sleep_seconds): """Execute a slow query without blocking other green threads.""" cur = conn.cursor() print(f"Query {query_id} starting") cur.execute("SELECT pg_sleep(%s), %s as id", (sleep_seconds, query_id)) result = cur.fetchone() print(f"Query {query_id} completed") return result # Create a green thread pool pool = eventlet.GreenPool() # Spawn concurrent database queries pool.spawn(query_database, 1, 2) pool.spawn(query_database, 2, 2) pool.spawn(query_database, 3, 2) # Wait for all to complete - runs concurrently pool.waitall() ``` -------------------------------- ### psycogreen.eventlet.patch_psycopg() Source: https://context7.com/psycopg/psycogreen/llms.txt Configures psycopg2 to work with Eventlet in non-blocking mode. Call this after Eventlet monkey patching and before database connections. ```APIDOC ## psycogreen.eventlet.patch_psycopg() ### Description Configures psycopg2 to work with Eventlet in non-blocking mode by registering a wait callback that integrates with Eventlet's hub. Call this after Eventlet monkey patching and before database connections. ### Method Python Function Call ### Endpoint N/A ### Parameters None ### Request Example ```python import eventlet # Monkey patch standard library first eventlet.monkey_patch() # Patch psycopg2 to work with eventlet import psycogreen.eventlet psycogreen.eventlet.patch_psycopg() import psycopg2 conn = psycopg2.connect("dbname=mydb host=localhost user=myuser password=mypass") def query_database(query_id, sleep_seconds): """Execute a slow query without blocking other green threads.""" cur = conn.cursor() print(f"Query {query_id} starting") cur.execute("SELECT pg_sleep(%s), %s as id", (sleep_seconds, query_id)) result = cur.fetchone() print(f"Query {query_id} completed") return result # Create a green thread pool pool = eventlet.GreenPool() # Spawn concurrent database queries pool.spawn(query_database, 1, 2) pool.spawn(query_database, 2, 2) pool.spawn(query_database, 3, 2) # Wait for all to complete - runs concurrently pool.waitall() ``` ### Response N/A (This is a configuration function) ### Response Example N/A ``` -------------------------------- ### psycogreen.gevent.patch_psycopg() Source: https://context7.com/psycopg/psycogreen/llms.txt Configures psycopg2 to work with gevent in non-blocking mode. This function must be called after gevent monkey patching and before creating any database connections. ```APIDOC ## psycogreen.gevent.patch_psycopg() ### Description Configures psycopg2 to work with gevent in non-blocking mode by registering a wait callback that integrates with gevent's event loop. This function must be called after gevent monkey patching and before creating any database connections. ### Method Python Function Call ### Endpoint N/A ### Parameters None ### Request Example ```python import gevent import gevent.monkey # Monkey patch standard library first gevent.monkey.patch_all() # Patch psycopg2 to work with gevent import psycogreen.gevent psycogreen.gevent.patch_psycopg() import psycopg2 # Now database operations will cooperate with other greenlets conn = psycopg2.connect("dbname=mydb host=localhost user=myuser password=mypass") def query_database(query_id, sleep_seconds): """Execute a slow query without blocking other greenlets.""" cur = conn.cursor() print(f"Query {query_id} starting") cur.execute("SELECT pg_sleep(%s), %s as id", (sleep_seconds, query_id)) result = cur.fetchone() print(f"Query {query_id} completed") return result # Run multiple queries concurrently jobs = [ gevent.spawn(query_database, 1, 2), gevent.spawn(query_database, 2, 2), gevent.spawn(query_database, 3, 2), ] # All queries run concurrently, total time ~2 seconds instead of ~6 gevent.joinall(jobs) ``` ### Response N/A (This is a configuration function) ### Response Example N/A ``` -------------------------------- ### psycogreen.gevent.gevent_wait_callback(conn, timeout=None) Source: https://context7.com/psycopg/psycogreen/llms.txt Low-level wait callback function that integrates psycopg2 with gevent's event loop. This is automatically registered by `patch_psycopg()`, but can be used directly for custom implementations or when you need to pass a specific timeout. ```APIDOC ## psycogreen.gevent.gevent_wait_callback(conn, timeout=None) ### Description Low-level wait callback function that integrates psycopg2 with gevent's event loop. This is automatically registered by `patch_psycopg()`, but can be used directly for custom implementations or when you need to pass a specific timeout. ### Method Python Function Call ### Endpoint N/A ### Parameters - **conn** (psycopg2.extensions.connection) - The database connection object. - **timeout** (float, optional) - The maximum time in seconds to wait for I/O. Defaults to None. ### Request Example ```python import gevent.monkey gevent.monkey.patch_all() from psycopg2 import extensions from psycogreen.gevent import gevent_wait_callback # Manually register the wait callback (alternative to patch_psycopg) extensions.set_wait_callback(gevent_wait_callback) import psycopg2 # Connection will now use the registered callback for I/O waits conn = psycopg2.connect("dbname=postgres") cur = conn.cursor() cur.execute("SELECT 1") print(cur.fetchone()) # Output: (1,) ``` ### Response N/A (This is a callback function) ### Response Example N/A ``` -------------------------------- ### Patch psycopg2 for gevent Source: https://context7.com/psycopg/psycogreen/llms.txt Configures psycopg2 to work with gevent. Must be called after monkey patching and before establishing database connections. ```python import gevent import gevent.monkey # Monkey patch standard library first gevent.monkey.patch_all() # Patch psycopg2 to work with gevent import psycogreen.gevent psycogreen.gevent.patch_psycopg() import psycopg2 # Now database operations will cooperate with other greenlets conn = psycopg2.connect("dbname=mydb host=localhost user=myuser password=mypass") def query_database(query_id, sleep_seconds): """Execute a slow query without blocking other greenlets.""" cur = conn.cursor() print(f"Query {query_id} starting") cur.execute("SELECT pg_sleep(%s), %s as id", (sleep_seconds, query_id)) result = cur.fetchone() print(f"Query {query_id} completed") return result # Run multiple queries concurrently jobs = [ gevent.spawn(query_database, 1, 2), gevent.spawn(query_database, 2, 2), gevent.spawn(query_database, 3, 2), ] # All queries run concurrently, total time ~2 seconds instead of ~6 gevent.joinall(jobs) ``` -------------------------------- ### psycogreen.eventlet.eventlet_wait_callback(conn) Source: https://github.com/psycopg/psycogreen/blob/master/README.rst A wait callback function designed to integrate with the Eventlet event loop for asynchronous database operations. ```APIDOC ## Function psycogreen.eventlet.eventlet_wait_callback(conn) ### Description A wait callback integrating with Eventlet events loop. ### Method Python Function Call ### Endpoint N/A ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python # This function is intended to be registered internally by patch_psycopg() # Example usage is within the context of psycogreen.eventlet.patch_psycopg() ``` ### Response #### Success Response (None) This function is a callback and does not return a value directly. #### Response Example N/A ``` -------------------------------- ### psycogreen.eventlet.patch_psycopg() Source: https://github.com/psycopg/psycogreen/blob/master/README.rst Enables asynchronous processing in Psycopg when integrated with the Eventlet event loop. This function registers the eventlet_wait_callback() as a psycopg2 wait callback. ```APIDOC ## Function psycogreen.eventlet.patch_psycopg() ### Description Enable async processing in Psycopg integrated with the Eventlet events loop. It is performed by registering `eventlet_wait_callback()` as psycopg2 wait callback. ### Method Python Function Call ### Endpoint N/A ### Parameters None ### Request Example ```python import psycogreen.eventlet psycogreen.eventlet.patch_psycopg() ``` ### Response #### Success Response (None) This function does not return a value. #### Response Example N/A ``` -------------------------------- ### psycogreen.gevent.gevent_wait_callback(conn) Source: https://github.com/psycopg/psycogreen/blob/master/README.rst A wait callback function designed to integrate with the gevent event loop for asynchronous database operations. ```APIDOC ## Function psycogreen.gevent.gevent_wait_callback(conn) ### Description A wait callback integrating with gevent events loop. ### Method Python Function Call ### Endpoint N/A ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python # This function is intended to be registered internally by patch_psycopg() # Example usage is within the context of psycogreen.gevent.patch_psycopg() ``` ### Response #### Success Response (None) This function is a callback and does not return a value directly. #### Response Example N/A ``` -------------------------------- ### Register gevent wait callback manually Source: https://context7.com/psycopg/psycogreen/llms.txt Low-level registration of the wait callback for custom implementations or specific timeout requirements. ```python import gevent.monkey gevent.monkey.patch_all() from psycopg2 import extensions from psycogreen.gevent import gevent_wait_callback # Manually register the wait callback (alternative to patch_psycopg) extensions.set_wait_callback(gevent_wait_callback) import psycopg2 # Connection will now use the registered callback for I/O waits conn = psycopg2.connect("dbname=postgres") cur = conn.cursor() cur.execute("SELECT 1") print(cur.fetchone()) # Output: (1,) ``` -------------------------------- ### psycogreen.gevent.patch_psycopg() Source: https://github.com/psycopg/psycogreen/blob/master/README.rst Enables asynchronous processing in Psycopg when integrated with the gevent event loop. This function registers the gevent_wait_callback() as a psycopg2 wait callback. ```APIDOC ## Function psycogreen.gevent.patch_psycopg() ### Description Enable async processing in Psycopg integrated with the gevent events loop. It is performed by registering `gevent_wait_callback()` as psycopg2 wait callback. ### Method Python Function Call ### Endpoint N/A ### Parameters None ### Request Example ```python import psycogreen.gevent psycogreen.gevent.patch_psycopg() ``` ### Response #### Success Response (None) This function does not return a value. #### Response Example N/A ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.