Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Database Pooling with psycopg2

Below is a database pooling example. I don't understand the following.

  1. Why the getcursor function use "yield"?
  2. What is the context manager?

    from psycopg2.pool import SimpleConnectionPool
    from contextlib import contextmanager
    
    dbConnection = "dbname='dbname' user='postgres' host='localhost' password='postgres'"
    
    # pool define with 10 live connections
    connectionpool = SimpleConnectionPool(1,10,dsn=dbConnection)
    
    @contextmanager
    def getcursor():
        con = connectionpool.getconn()
        try:
            yield con.cursor()
        finally:
            connectionpool.putconn(con)
    
    def main_work():
        try:
            # with here will take care of put connection when its done
            with getcursor() as cur:
                cur.execute("select * from \"TableName\"")
                result_set = cur.fetchall()
    
        except Exception as e:
            print "error in executing with exception: ", e**
    
like image 203
user1187968 Avatar asked Oct 30 '25 21:10

user1187968


1 Answers

Both of your questions are related. In python context managers are what we're using whenever you see a with statement. Classically, they're written like this.

class getcursor(object):
    def __enter__(self):
        con = connectionpool.getconn()
        return con

    def __exit__(self, *args):
        connectionpool.putconn(con)

Now when you use a context manager, it calls the __enter__ method on the with statement and the __exit__ method when it exits the context. Think of it like this.

cursor = getcursor()
with cursor as cur: # calls cursor.__enter__()
   cur.execute("select * from \"TableName\"")
   result_set = cur.fetchall()
# We're now exiting the context so it calls `cursor.__exit__()` 
# with some exception info if relevant
x = 1

The @contextmanager decorator is some sugar to make creating a context manager easier. Basically, it uses the yield statement to give the execution back to the caller. Everything up to and including the yield statement is the __enter__ method and everything after that is effectively the __exit__ statement.

like image 157
Tim Martin Avatar answered Nov 01 '25 11:11

Tim Martin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!