mirror of
https://github.com/psycopg/psycopg2.git
synced 2025-08-04 04:10:09 +03:00
lib/pool -- add convenience method for acquiring a cursor as a context
This commit is contained in:
parent
fe4cb0d493
commit
4803870f90
|
@ -26,6 +26,7 @@ This module implements thread-safe (and not) connection pools.
|
|||
|
||||
import psycopg2
|
||||
import psycopg2.extensions as _ext
|
||||
from contextlib import contextmanager
|
||||
|
||||
|
||||
class PoolError(psycopg2.Error):
|
||||
|
@ -122,6 +123,14 @@ class AbstractConnectionPool(object):
|
|||
del self._used[key]
|
||||
del self._rused[id(conn)]
|
||||
|
||||
@property
|
||||
@contextmanager
|
||||
def quick_cursor(self):
|
||||
"""A ContextManager for quickly getting a cursor"""
|
||||
with self.getconn() as conn, conn.cursor() as cur:
|
||||
yield cur
|
||||
self.putconn(conn)
|
||||
|
||||
def _closeall(self):
|
||||
"""Close all connections.
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user