New to Connection Pooling

时间秒杀一切 提交于 2021-02-11 14:51:15

问题


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)
    

回答1:


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.



来源:https://stackoverflow.com/questions/65117339/new-to-connection-pooling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!