0

I'd like to make an python application and retrieve query results from mysql server.

So every time when an event happened, the application connect to mysql through mysqlclient like below

# retrieve query result from db
db=_mysql.connect(host="localhost",user="root",
                  passwd="password",db="db")

Is there a way not to try to connect every time but keep the connection?

7
  • 1
    What the event is? You can connect to database once at the beginning of the python script, and use it after that. If you re-run the python script when every event arrive, it must re-connect to the database. Commented Dec 16, 2020 at 6:34
  • 1
    Did you tried connection pooling? Commented Dec 16, 2020 at 6:41
  • @ElapsedSoul the event will be a message from MQ Commented Dec 16, 2020 at 6:51
  • @Sajidshaikh no i didn't. what is that? is there any documentation for connection pooling? Commented Dec 16, 2020 at 6:51
  • 1
    I read about it here Commented Dec 16, 2020 at 6:52

1 Answer 1

1

Change

begin loop:
    get MQ msg
    db=_mysql.connect(host="localhost",user="root",
                  passwd="password",db="db")
    db.insert()

to

db=_mysql.connect(host="localhost",user="root",
                  passwd="password",db="db")
begin loop:
    get MQ msg
    db.insert()
Sign up to request clarification or add additional context in comments.

1 Comment

Additional remarks, the connection pool Sajidshaikh mentioned help you to manage the connection like parallel process and re-connection. If you care about the stability of the connection, you can use it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.