persistent python Mysqldb connection

Asked

Viewed 158 times

0

Because I have to keep opening and closing database connection memory consumption goes up? I wonder if I use an open connection always, I will have the advantage of decreasing memory consumption.

import MySQLdb

dblocal = MySQLdb.connect("host",
                        "user",
                        "password", 
                        "db")

cursor = dblocal.cursor()
cursor.execute("SELECT * FROM ("tabela")
recordsdblocal = cursor.fetchall()

if it’s possible and viable how can I do it?

UTILIZANDO UM ORANGE PI ZERO 256MB:

  • Why would consumption rise from opening and closing the connection? Have you tested both ways to see the difference? Not that I need to, but if I could post a few numbers to support the question, and maybe explain what was the difficulty in keeping the connection open, it would be more productive to attract answers that focus on the core of the real problem you’re having.

  • The big problem is that I don’t know how to work with the connection always open. Could you show me how to do this to work analyze the difference.

  • Basically it would just not close. Open with connect at the beginning 1x only and save in dblocal as you already did at the beginning, then always use the same.dblocal variable on all cursors. Only release each cursor after use (or see in the documentation if you can reuse the cursor too - qq way you have to release the results you no longer need)

  • I took a look at the documentation. I didn’t see how to take advantage of a cursor.

  • I think just releasing each cursor explicitly somehow helps. Anyway, let’s wait for someone who has practice with Mysqldb to give more concrete examples. The central point here is to reuse connect.

  • 1

    Tetando Working like this. Amigo has already helped a lot. thank you very much Waiting for the answer from other colleagues.

  • Already gave a survey on Connection pooling with python? http://eventlet.net/doc/modules/db_pool.html

  • No. Now that you’ve spoken I’ll take a look. VLW

Show 3 more comments
No answers

Browser other questions tagged

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