简体   繁体   中英

New to Connection Pooling

So I am new to connection pooling. I am trying to determine how to use a pool in order to speed up my query. I have a query that works, but I dont think I am using the pool correct. Here is the syntax, if you see anywhere I could be more efficient please let me know.


try:
    db=mysql.connector.connect(poolname="mypool", pool_size=10, **config)
    
    cursor.execute(query1)
    df1=create_df(cursor)
    
    cursor.execute(query2)
    df2=create_df(cursor)
    
    cursor.execute(query3)
    df3=create_df(cursor)
    

Your question didn't make it clear how cursor comes from db .

Consider using sqlalchemy . Then you get automatic pooling for free.

import pandas as pd
import sqlalchemy as sa

engine = sa.create_engine(your_local_mysql_url_with_credentials)
with engine.connect() as con:
    df1 = pd.read_sql(query1, con)
    df2 = pd.read_sql(query2, con)
    df3 = pd.read_sql(query3, con)

The pool winds up being an attribute of engine . In practice you'll seldom care about inspecting it, since it Just Works, hanging onto a server TCP connection across queries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM