How to create a Connection Pool using python and sqlAlchemy?

Asked

Viewed 290 times

3

Hello ! Well I am studying python and I started looking for material on Connection pool using sqlalchemy but unfortunately I did not find any example on the internet that shows the process of creating the Connection pool using python and sqlalchemy. If you know good materials please indicate me. Thanks in advance !

  • Why not start with the documentation? http://docs.sqlalchemy.org/en/latest/core/pooling.html

  • I’ve done it but I don’t understand it right

1 answer

1

Follow the documentation page: http://docs.sqlalchemy.org/en/latest/core/pooling.html

Take an example:

engine = create_engine('postgresql://me@localhost/mydb',
                       pool_size=20, max_overflow=0)

If you only want the sqlalchemy pool system and the native drive:

import sqlalchemy.pool as pool
import psycopg2

def getconn():
    c = psycopg2.connect(username='ed', host='127.0.0.1', dbname='test')
    return c

mypool = pool.QueuePool(getconn, max_overflow=10, pool_size=5)
# get a connection
conn = mypool.connect()

# use it
cursor = conn.cursor()
cursor.execute("select foo")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.